Android custom toast example

In this post, we will discuss about how to create a custom toast message in Android application. We usually see toast as a simple text message which pops up on the screen.

But we can create custom toast with images and more style attributes like padding, gravity, etc.You can create custom toast message shown below at the end of the post:

What is Toast?

A toast is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user’s current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events. Read more at Android developer site

What are we going to develop?

We are going to develop a simple android activity which will have one button at the center of the screen. On clicking the button, custom toast message will be appeared.

Quick Links

Project Structure

  • Create new android project [File >> New >> Android Project] with project name CustomToastExample
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘’
  • Click finish

Code Listings

Layout creation:
We need to create two layouts:

  1. Layout of the application – main.xml
  2. Layout of the custom toast message – toast_custom_layout.xml
Application layout – main.xml:
Open main.xml under /res/layout and add replace it with the below XML.
<LinearLayout xmlns:android=""
    android:padding="10dp" >

        android1:text="Show custom toast" />


Custom toast layout –  toast_custom_layout.xml:

Create new layout XML under /res/layout with name toast_custom_layout.xml and the below XML.

<LinearLayout xmlns:android=""
    android:padding="10dp" >

        android1:src="@drawable/red_heart" />

        android1:text="This is custom Toast message"
        android1:textAppearance="?android:attr/textAppearanceMedium" />


We need to inflate the custom toast xml into Toast view:

Following snippet does the job of creating and showing custom toast.

//Retrieve the layout inflator
LayoutInflater inflater = getLayoutInflater();
//Assign the custom layout to view
//Parameter 1 - Custom layout XML
//Parameter 2 - Custom layout ID present in linearlayout tag of XML
View layout = inflater.inflate(R.layout.toast_custom_layout,
			(ViewGroup) findViewById(;
//Return the application context
Toast toast = new Toast(getApplicationContext());
//Set toast gravity to bottom
toast.setGravity(Gravity.BOTTOM, 0, 0);
//Set toast duration
//Set the custom layout to Toast
//Display toast;
Here is the Activity code of this application.


import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class CustomToastExampleActivity extends Activity {
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
                //Find the button
		Button btn = (Button) findViewById(;
                //Create onclick listener class
		btn.setOnClickListener(new View.OnClickListener() {
                        //When button is clicked, toast will be showed
			public void onClick(View v) {
				LayoutInflater inflater = getLayoutInflater();
				View layout = inflater.inflate(R.layout.toast_custom_layout,
						(ViewGroup) findViewById(;
				Toast toast = new Toast(getApplicationContext());
				toast.setGravity(Gravity.BOTTOM, 0, 0);



Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:


Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.



Android Toast

I hope you enjoyed the post!! 🙂

Suggested posts for further reading


Related Articles

Author: Udhay

Share This Post On
468 ad
  • ftxbird

    Nice toast ?

  • XYZ


  • HI,

    It is very useful for me to understand how to write the code for toast messages. Is it possible to remove the toast message from bar or any other settings in android?

  • ornay

    why are you using two different namespaces? android1 & android?

    • Hi, That doesn’t matter. You just use single namespace.

  • good one. we can see other simple toast example in

  • Coder

    How do I change the text code dynamically ? For each toast it should set something new from the database.

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest