• Have Any Question?
  • contact@eitworld.com

Android Toast

    • In android, toast is like a notification or like a simple popup message that appears on the screen and disappears automatically after some time.
    • For example, when we send any message to anyone then one message shown to us “Message Sent” and this message disappears after some seconds; it is known as toast.
    • In android, toast size is equal to the message and we cannot perform any action on the toast but we can perform action on that activity on which the toast displays.
    • The toast message automatically fades in and out and it does not accept any user interaction.
    • To use the toast, first we have to create the instantiate of the Toast with the makeText() method. This method takes three arguments: the application Context, the text message, and time duration for the toast for display on the screen. It returns an initialized Toast object.
    • show() method is used to show the toast notification on the current activity.
    • The syntax of makeText() method is as follows:
public static Toast makeText(contextApplication, Message,duration);
    • The example of the toast is as follows:
Context context = getApplicationContext();
CharSequence text = “Hello eitworld”;
int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeToast(context,text,duration);
// another way to use this toast

    • In the above example, first we have declared the instance of the Context and initialized with the current application context. getApplicationContext() method is used to get the application context.
    • After that we have assigned a message into text of CharSequence type.
    • Then we have specified the time duration for the message and Toast.LENGTH_LONG is used for time limit and assigned to the duration of type int.
    • show() method id used to show the toast message on the current activity.
    • Now let see this example using button click event.
    • The xml file for this demo is shown below:
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent” android:layout_height=”match_parent”
tools:context=”.MainActivity” >

android:text=”Toast Example” />

android:text=”Show Toast” />


    • In this layout file, we have used relative layout.
    • android:id=”@+id/textView1″ is used to define the id of the message that is shown on the activity.
    • android:id=”@+id/btnToast” is used to define the id of the button so that it can be identified in the java file easily.
    • The java file is as follows:
package com.example.androidtoast;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
// create button instance
Button tost;

protected void onCreate(Bundle savedInstanceState) {

// register button
tost = (Button) findViewById(R.id.btnToast);

//perform action on button click
tost.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
CharSequence text = “This is example of the Toast”;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);

// we can perform this action within one line
Toast.makeText(getApplicationContext(), “eitworld.com”, Toast.LENGTH_LONG).show();

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

    • In this java file first we have to import some packages which are as follows:


    • In this android.widget.Toast package is used to define the toast related functionality.
    • After importing the package, we have to create the instance of the button so that we can register the button as follows:


    • Here we declare an instance variable tost of type Button.
    • Now we have to register the button as follows:


    • Here we are registering the button using the id of the button which is defined in the xml layout file.
    • Now we have to perform action on the button as follows:


    • Here we have used setOnClickListener() method which takes OnClickListener object and creates an anonyms method.
    • Inside this method, we must override the onClick() method which takes View type parameter to view.
    • Inside method, we have used Context type of instance to assign the current context of the application.
    • After that we have used CharSequence type variable to hold the message.
    • After that we have to specify the time duration for the toast so we have used duration variable of int type.
    • Now we have used the following method and we have passed these variables to the method as parameters as follows:


    • Now we have to show the toast using the show() method as follows:


    • We can show the toast using one line of code as follows:


    • In this, we have passed the parameters to the method directly. This will execute after the first toast.
    • Here first parameter getApplicationContext() is used to get the context of application.
    • After that we can assign the text message as above shown.
    • After that we have to specify the time for toast, here we have used Toast.LENGTH_LONG.
    • And after that we have to use the show() method shown above.
    • Output of this demo is as follows:




Real Time Web Analytics