Notifications are generally used to make user aware about an event that has occurred in your application. Once notified the notification may give an option to interact with itself or not. There are three types of notifications in Android.

  • Toast Notifications
  • Status Bar Notifications
  • Dialog Notifications

In this tutorial we will be covering Toast Notification and how you can implement it in your application.

Toast Notification is used to display a confirmation message to the user to which he/she cannot respond. It popups on the screen for a short duration of time and then disappears. It covers only the space required for the message to display on the screen and user's current activity remains active and he/she can interact with it. Toast Notification can be delivered even when your application is not visible via a background service. The length of the message is generally very short such as "Alarm has been setup successfully" or "File Saved".

Note:

  • We will be using Android Platform 1.5 for developing our application.

To demonstrate how to use Toast Notification we will use a simple base application.

  • Name of the application - Toast Notification
  • Package Name - com.botskool.ToastNotification
  • Activity - ToastNotification

Below is the source code for the various file that will be used in this application -

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView  
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast Notification"
android:id="@+id/toastbutton"
/>
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="hello">Click on the button below to see Toast Notification</string>
   <string name="app_name">Toast Notification</string>
</resources>

ToastNotification.java

package com.botskool.ToastNotification;


import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;


public class ToastNotification extends Activity implements OnClickListener {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Set a Click Listener on the Toast Button

Button toastnotify = (Button) findViewById(R.id.toastbutton);

toastnotify.setOnClickListener(this);


}

public void onClick(View view) {

// Check if the Toast button has been clicked

if (view == findViewById(R.id.toastbutton)) {

// Some Code here which will display the toast popup window

}

}

}

Toast Notification application in Android

This is the basic skeleton of our sample application. We have here registered a Click Listener event for the button Show Toast Notification. When a user clicks on this button onClick() function is called and in this we check whether the clicked button is Show Toast Notification button Notification or not. We have yet not added code for showing toast notification.

To display toast notification message we will first instantiate a Toast object with makeText() function. This function takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object. We can then display the toast notification with show() function. Using chaining of functions our code will be -

Toast.makeText(context, text, duration).show();

Toast.makeText(this, "This is a sample Toast Notification message", Toast.LENGTH_SHORT).show();

So just add this line of code in the onClick() function and the complete code of the TestNotification.java will be -

package com.botskool.ToastNotification;


import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;


public class ToastNotification extends Activity implements OnClickListener {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Set a Click Listener on the Toast Button

Button toastnotify = (Button) findViewById(R.id.toastbutton);

toastnotify.setOnClickListener(this);


}

public void onClick(View view) {

// which button is clicked?

// the Toast button

if (view == findViewById(R.id.toastbutton)) {

// display the toast popup window

Toast.makeText(this, "This is a sample Toast Notification message", Toast.LENGTH_SHORT).show();

}

}

}

Now run your Android Application and click on Show Toast Notification button. You will see a notification message towards the bottom of the screen as shown below.

Toast Notification Message in Android

Enjoy!

Download source code.