Monday, June 17, 2013

Creating your Custom Toast View in Android

You can create a customised layout for your toast notification. Its pretty easy.
To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView(View) method.
For example, you can create the layout for the toast with the following XML (saved as toast_new_layout.xml):

<LinearLayout xmlns:android=""
    <ImageView android:src="@drawable/droid"
    <TextView android:id="@+id/text"

Notice that the ID of the LinearLayout element is "toast_layout_r". You must use this ID to inflate the layout from the XML, as shown here:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                               (ViewGroup) findViewById(;
TextView text = (TextView) layout.findViewById(;
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

First, retrieve the LayoutInflater with getLayoutInflater() (or getSystemService()), and then inflate the layout from XML using inflate(int, ViewGroup). The first parameter is the layout resource ID and the second is the root View. You can use this inflated layout to find more View objects in the layout, so now capture and define the content for the ImageView and TextView elements. Finally, create a new Toast withToast(Context) and set some properties of the toast, such as the gravity and duration. Then callsetView(View) and pass it the inflated layout. You can now display the toast with your custom layout by calling show().
Note: Do not use the public constructor for a Toast unless you are going to define the layout withsetView(View). If you do not have a custom layout to use, you must use makeText(Context, int, int)to create the Toast.
For more reference visit:
Screen-shot of  a custom toast displayed while outgoing calls.

You can customize the design of toast layout depending on your needs like the above one.