What is Toast message in Android?

Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime.

The android.widget.Toast class is the subclass of java.lang.Object class.

You can also create custom toast as well for example toast displaying image. You can visit next page to see the code for custom toast.


Toast class

Toast class is used to show notification for a particular interval of time. After sometime it disappears. It doesn’t block the user interaction.

Constants of Toast class

There are only 2 constants of Toast class which are given below.

ConstantDescription
public static final int LENGTH_LONGdisplays view for the long duration of time.
public static final int LENGTH_SHORTdisplays view for the short duration of time.

Methods of Toast class

The widely used methods of Toast class are given below.

MethodDescription
public static Toast makeText(Context context, CharSequence text, int duration)makes the toast containing text and duration.
public void show()displays toast.
public void setMargin (float horizontalMargin, float verticalMargin)changes the horizontal and vertical margin difference.

Android Toast Example

  1. Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT).show();  

Another code:

  1. Toast toast=Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT);  
  2. toast.setMargin(50,50);  
  3. toast.show();  

Here, getApplicationContext() method returns the instance of Context.


Full code of activity class displaying Toast

Let’s see the code to display the toast.File: MainActivity.java

  1. package example.javatpoint.com.toast;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.Toast;  
  6.   
  7. public class MainActivity extends AppCompatActivity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.   
  14.         //Displaying Toast with Hello Javatpoint message  
  15.         Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT).show();  
  16.     }  
  17. }  

Output:

android toast example output 1

Android Custom Toast Example

You are able to create custom toast in android. So, you can display some images like congratulations or loss on the toast. It means you are able to customize the toast now.


activity_main.xml

Drag the component that you want to display on the main activity.File: activity_main.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     tools:context=”example.javatpoint.com.customtoast.MainActivity”>  
  8.   
  9.     <TextView  
  10.         android:layout_width=”wrap_content”  
  11.         android:layout_height=”wrap_content”  
  12.         android:text=”Hello World!”  
  13.         app:layout_constraintBottom_toBottomOf=”parent”  
  14.         app:layout_constraintLeft_toLeftOf=”parent”  
  15.         app:layout_constraintRight_toRightOf=”parent”  
  16.         app:layout_constraintTop_toTopOf=”parent” />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  

customtoast.xml

Create another xml file inside the layout directory. Here we are having ImageView and TextView in this xml file.File: customtoast.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=”match_parent”  
  4.     android:layout_height=”match_parent”  
  5.     android:id=”@+id/custom_toast_layout”  
  6.     android:orientation=”vertical”  
  7.     android:background=”#F14E23″  
  8.     >  
  9.   
  10.     <ImageView  
  11.         android:id=”@+id/custom_toast_image”  
  12.         android:layout_width=”wrap_content”  
  13.         android:layout_height=”wrap_content”  
  14.         android:contentDescription=”Hello world”  
  15.         android:src=”@drawable/jtp_logo”/>  
  16.   
  17.     <TextView  
  18.         android:id=”@+id/custom_toast_message”  
  19.         android:layout_width=”wrap_content”  
  20.         android:layout_height=”wrap_content”  
  21.         android:contentDescription=”To”  
  22.         android:text=”JavaTpoint custom Toast” />  
  23. </LinearLayout>  

Activity class

Now write the code to display the custom toast.File: MainActivity.java

  1. package example.javatpoint.com.customtoast;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.   
  18.         //Creating the LayoutInflater instance  
  19.         LayoutInflater li = getLayoutInflater();  
  20.         //Getting the View object as defined in the customtoast.xml file  
  21.         View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));  
  22.   
  23.         //Creating the Toast object  
  24.         Toast toast = new Toast(getApplicationContext());  
  25.         toast.setDuration(Toast.LENGTH_SHORT);  
  26.         toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  
  27.         toast.setView(layout);//setting the view of custom toast layout  
  28.         toast.show();  
  29.     }  
  30. }  

Output:

android custom toast example output 1
android custom toast example output 2

About Author /

Hi everyone. This is Vishal Yadav, & I'm an Android developer at Techwarezen pvt. ltd. and here I'm sharing my experience and knowledge with you which I have learnt. Hope It may help you improving yourself. Thank you! *Note - Please specify if I uploaded any wrong content.*

Leave a Comment

Your email address will not be published.

Start typing and press Enter to search