Android Toast


This article will guide you how to show android Toast i.e. a visual prompt message for users which disappears automatically after a pre-defined time.


DownloadSource Code :


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Toast which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/Toast/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have. 





package com.nestedif.toast;

import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		//	Toast with simple String
		Toast.makeText(getApplicationContext(), "Enjoying Android, isn't it ?", Toast.LENGTH_LONG).show();
		//	Toast with String variable		
		String text_for_toast = "Using String Variable to display Toast" ;
		Toast.makeText(getApplicationContext(), text_for_toast, Toast.LENGTH_LONG).show();
		//	Toast with numeric data		
		int data_for_toast = 123456 ;
		Toast.makeText(getApplicationContext(), data_for_toast + "", Toast.LENGTH_LONG).show();


Explaining above Code :

  • Android Toast follows syntax :  Toast.makeText( Context , String Text , Duration ).show(); Over here we pass 3 parameters to makeText() method of Toast which are shown below and we add show() to make our Toast visible.
    1. context holds information about your application’s environment. It lets newly created objects understand what has been going on. You can get context by invoking  this (when working itself in activity class) or getApplicationContext() or getBaseContext() or getContext()
    2. String Text is the matter which we want to display using Toast.
    3. Duration specifies time to remain visible via  Toast.LENGTH_LONG or Toast.LENGTH_SHORT.
  • Line 17 We have made android Toast which is passed with context using getApplicationContext() we can also use ‘this’ over here, next we have passed a string message directly and third parameter Toast.LENGTH_LONG to display it for long duration.
  • Line 22 All our parameters are same, just instead of a direct string we have passed a String variable.
  • Line 27 Our 3rd Toast is another case scenario where we have passed an Integer variable. Observe that we have appended a null(blank) string after that. The reason is that Toast requires string parameter. Try removing the null string and you will find that our App will crash.



2. activity_main.xml : Layout file

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="Example of Android Toast. \nYou will find Toast Below" />



  • For displaying Android Toast we don’t need  to add any separate thing inside our activity_main.xml Layout file.




Final Output :

android toast



Trouble-Shooting :

  • Application Crashes ( Error : Unable to start activity…..   Resource not found Exception : String resource )
    Check that you have written proper string variable or string value inside Text area and if you are having just numeric data  then append a null string to regard whole data as a string. Also make sure whether your Toast receives proper Context or not.



Report Errors + Bugs & Become Insider for

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to you could help other peers.