Toast Notification in Android with a simple example

A Toast Notification is just a pop up message to notify the user of some event. It just remains on the screen for some time and fades away. The user can’t interact with the toast message, that is, it doesn’t receive any user response and we can still interact with our activity when it’s still on the screen.

Let’s see how a Toast Notification can be created.

First of all, create an instance of the Toast object as shown below:

Toast toast=Toast.makeText(getApplicationContext(), "This is a Toast",Toast.LENGTH_LONG );

Here, the makeText() has got three parameters:

1)Context – the Application context

2)Text – the Text Message that needs to be displayed(here its “This is a Toast”)

3)Duration – the duration for the Toast to be displayed

Thus, the object ‘toast’ has got the details for a simple Toast.

Now, We need to display the Toast and for that we need a method named show().

The code for display is simple:

toast.show();

We can also position the Toast as we desire using a method named setGravity() which has got three parameters:-gravity,x-offset,y-offset.

For example:

toast.setGravity(Gravity.TOP, 40, 30);

Note: By default the Toast appears at the bottom, centred horizontally. So if no need of setGravity() method if you don’t want to position the Toast.

The Whole code is given below:

package com.deepthi.mytoast;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyToastDemo extends Activity {
    /** Called when the activity is first created. */
                Button toastbtn1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        toastbtn1=(Button)findViewById(R.id.btntoast1);
        toastbtn1.setOnClickListener(new OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                                // TODO Auto-generated method stub
                                                                Toast toast=Toast.makeText(getApplicationContext(), "This is a Toast",Toast.LENGTH_LONG );
                                                                toast.setGravity(Gravity.TOP, 40, 30);
                                                                toast.show();
                                                }
                                });
    }
}

The XML Layout:

 <?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" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/btntoast1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast" />
</LinearLayout>

The Output looks like this:

, , , , , , , , ,

  1. #1 by Narendra on October 11, 2012 - 11:11 pm

    good example for freshers…………thank you alot……………………..

  2. #2 by Darshan Shah on February 1, 2013 - 11:38 am

    Good Examples Related To Android..

  3. #3 by developer on April 22, 2014 - 12:34 pm

    In this case toast is shown in the very same xml page where the button is placed, is there any way that I can show the Toast in a xml page other than the xml page where the button is placed.

    Thank you

  1. A Simple ListView in Android « Android Desk

Leave a comment