Toast in Android :

Toast is transient notifications that stay visible for less than a few seconds before fading out. Toast don’t take focus and its not a modal , so that they don’t interrupt the application.You can also use it for debugging purpose. A toast provides simple feedback about an operation in a small popup.

android-custom-toast-example

Toasts are excellent for informing your users of events without forcing them to open an Activity or browse a Notification. they provide a perfect mechanism for alerting users to events occurring in background Services without interrupting foreground applications. Generally, your application should show Toasts only if one of its Activities is active.

Creating Simple (Default) Android Toast :

The Toast class includes a static makeText method that makes a standard Toast display window.

1. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

2. Open “res/layout/activity_main.xml” file and put this xml code it into file:

File : res/layout/activity_main.xml

In Android create a new Toast, pass the current Context, the text message to show, and also the length of your time to show it (LENGTH_SHORT or LENGTH_LONG) into the makeText method. once making a Toast, you’ll be able to show it by calling show.

Output of  Android Default Toast in emulator :

android simple toast example


Creating a Custom Toast View :

Sometimes a simple text message is not enough, you’ll be able to make a customized layout for your toast notification. To make 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.

1. Create “res/layout/mycustom_toast.xml” file and put this xml code it into file:

File : res/layout/mycustom_toast.xml

You can also create  new  border color, radius etc,

2. Create “res/drawable/mycustom_toast_border.xml” file : (as show in figure)

android custom toast example 1

File : res/drawable/mycustom_toast_border.xml

Now add code for create Custom Toast in java, that Layout of the Toast is “mytoast.xml” call by View.

3. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

That Code contain 4 Toast Codes

Output of  Android Custom Toast in emulator :

android custom toast example

 

Download Android Simple and Custom Toast example Source code :

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 23.0

  1. In case someone is still wondering how to set up text dynamically, here is how:

    Context context=getApplicationContext();
    LayoutInflater inflater=getLayoutInflater();

    View customToastroot =inflater.inflate(R.layout.toast_warning, null);

    Toast customtoast=new Toast(context);

    //you have to find a ID of the textview from your toastlayout, in this case its done somehow different since in my //application im looking for a view from outside of an activity
    //Then you just set the text of your textview and set up the toast.

    TextView messageText = (TextView)customToastroot .findViewById(R.id.toast_warning_message);
    messageText.setText(“SOME MESSAGE”);

    toast.setView(root);
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();

  2. Hi.
    I’m not able to show my dynamic text.

    Works perfect following the example for the static text;

    This is my code (not exactly like yours because I have the code into a fragment):

    View customToastroot = getActivity().getLayoutInflater().inflate(R.layout.custom_toast, null);

    Toast customtoast=new Toast(getContext());

    TextView title = (TextView)customToastroot.findViewById(R.id.textView1);
    title.setText(“My dynamic title”);

    customtoast.setView(customToastroot);
    customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);
    customtoast.setDuration(Toast.LENGTH_LONG);
    customtoast.show();

    What am I doing wrong??
    Any help?

    Thanks

  3. Hi , Every things work fine but I also wanted click listener on the image. how can I do that..? setting the on clicklistener on imageview didn’t worked.. 🙁

Leave a Reply