Sun. Jul 3rd, 2022

Normal Toast can create only with simple text but we can customize Toast using custom layout. In custom layout we can define the design and pass root view object to the setView Method.

For example, you can create the layout for the toast visible in the screenshot to the right with the following XML (saved as layout/custom_toast.xml):

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

Notice that the ID of the LinearLayout element is “custom_toast_container”. You must use this ID and the ID of the XML layout file “custom_toast” to inflate the layout, 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 with Toast(Context) and set some properties of the toast, such as the gravity and duration. Then call setView(View) and pass it the inflated layout. You can now display the toast with your custom layout by calling show()

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published.