Custom Toast Notification in Android with an Example

In one of my previous articles I’ve discussed about Toast Notification. Now, I’ll show you how we create a Custom Toast in android with an example.

Here, I’ve created an Xml file named my_customtoast_xml in which I’ve included an AnalogClock, an ImageView and a TextView which I need in my Toast.

The Xml layout is shown below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/toastlayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#FFF"
 android:orientation="horizontal" >

 <AnalogClock
 android:id="@+id/analogClock1"
 android:layout_width="95dp"
 android:layout_height="98dp" />
 <ImageView
 android:id="@+id/img"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:layout_marginRight="15dp"/>
 <TextView
 android:id="@+id/txtvdisplay"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textColor="#00ff00"
 android:textSize="20dip"/>
</LinearLayout>

 

In my main.xml I’ve included a button and in its click event I’ve written the code for Custom Toast.

My main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <Button
 android:id="@+id/btntoast"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click to View My Custom Toast"
 />
</LinearLayout>

Now lets the coding part. First of all, we need to inflate the layout (here, toastlayout) from the XML (here,my_customtoast_xml). (Inflate means converting the Xml layout specifications into View Objects.)

This is done by using a method named inflate(int resource,ViewGroup root) where the resource is the Xml file resource and root is the ViewGroup object specified by the Xml file. But for that first we need to create an instance of the LayoutInflator class using either getSystemService() or getLayoutInflator() and usind this instance we will inflate the Xml layout as shown below.

LayoutInflater myInflator=getLayoutInflater();
 View myLayout=myInflator.inflate(R.layout.my_customtoast_xml,(ViewGroup)findViewById(R.id.toastlayout) );
 

 Now this inflated layout is used to find the View objects in the layout. Therefore, now we can find them out (here the TextView, ImageView and AnalogClock) and define their content as shown below.

ImageView myImage=(ImageView)myLayout.findViewById(R.id.img);
 myImage.setImageResource(R.drawable.kitty);
 TextView myMessage=(TextView)myLayout.findViewById(R.id.txtvdisplay);
 myMessage.setText("My Custom Toast");
 AnalogClock myClock=(AnalogClock)myLayout.findViewById(R.id.analogClock1);

Now comes the code for creating the Toast and the code is shown below:

Toast myToast=new Toast(getApplicationContext());
 myToast.setDuration(Toast.LENGTH_LONG);
 myToast.setView(myLayout);
 myToast.show();

Now let’s look at the above code , we can see that the properties of the Toast (here, Duration) are set and then using the Toast object setView(view) is called in which the inflated layout (here, myLayout) is passed and finally, to display the Toast, call the method show().

The whole code looks like this:

package com.deepthi.customtoast;
import android.app.Activity;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MyCustomToast extends Activity {
 /** Called when the activity is first created. */
 Button click;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 click=(Button)findViewById(R.id.btntoast);
 click.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View v) {
 //TODO Auto-generated method stub
 LayoutInflater myInflator=getLayoutInflater();
 View myLayout=myInflator.inflate(R.layout.my_customtoast_xml,(ViewGroup)findViewById(R.id.toastlayout) );
 ImageView myImage=(ImageView)myLayout.findViewById(R.id.img);
 myImage.setImageResource(R.drawable.kitty);
 TextView myMessage=(TextView)myLayout.findViewById(R.id.txtvdisplay);
 myMessage.setText("My Custom Toast");
 AnalogClock myClock=(AnalogClock)myLayout.findViewById(R.id.analogClock1);
 Toast myToast=new Toast(getApplicationContext());
 myToast.setDuration(Toast.LENGTH_LONG);
 myToast.setView(myLayout);
 myToast.show();
 }
 });

 }
}
 

The Output is shown below:

After Clicking the button:

Now, just try creating your own Toast.

, , , , , , , , ,

  1. #1 by 2013 Disney Vacation Club on February 25, 2012 - 7:26 pm

    I would like to learn more about this topic. I just think that this stuff is interesting.

  2. #2 by Deepthi G on February 25, 2012 - 7:56 pm

    Hey
    Thanks for those kind words dude..

  3. #3 by udi on September 9, 2012 - 5:54 pm

    very helpfull, thanks

  4. #4 by josh on June 21, 2013 - 4:34 pm

    good one. we can see other simple toast example in

    http://articlesforprogramming.blogspot.in/2013/06/toast-in-android.html

  5. #5 by Chirag Ghori on September 28, 2013 - 11:05 am

    thanks a lot…… its very helpfull to me…

  6. #6 by Ullas on September 24, 2014 - 11:26 am

    thanks a lot. Very very helpful

  7. #7 by Pankaj on February 19, 2015 - 7:40 pm

    how to add Button in toast? And How to perform onClickListener on that button?

  8. #8 by ARiF ISLAM on July 22, 2015 - 3:21 am

    Helpful. Thanks a lot.

  9. #9 by jayman on July 25, 2016 - 4:37 pm

    Its Good But Now try to Assign Show Toast method in Global class and call it in every class (Y)

  10. #10 by Sumit K on December 5, 2016 - 3:34 pm

    How to close the Toast of Activity close?

  11. #11 by Divya on April 8, 2017 - 11:11 am

    Really Helpful thanks

  12. #12 by ram bhawan on October 12, 2018 - 3:49 pm

    thank you! very helpful.

  1. How to Show Custom Toast in Android
  2. Custom toast on Android: a simple example - PhotoLens
  3. Custom toast on Android: a simple example – Flutter Fixes
  4. advantages of a dedicated server

Leave a comment