Toasts for Android Studio

A toast provides a simple popup message that is displayed on the current activity UI screen (e.g. Main Activity).

Example :

Syntax :

filter_none

edit
close

play_arrow

link
brightness_4
code

// to get Context
Context context = getApplicationContext(); 
// message to display
String text = "Toast message"
// toast time duration, can also set manual value 
int duration = Toast.LENGTH_SHORT; 
Toast toast = Toast.makeText(context, text, duration);
// to show the toast
toast.show(); 

chevron_right


We can also create toast with single line by passing variables directly to makeText() function. This method takes three parameters context, popup text message, the toast duration. After creating Toast object you can display the toast by using show() method.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

Toast.makeText(MainActivity.this
"Error"+ msg, Toast.LENGTH_SHORT).show();

chevron_right


Creating a Custom Toast :
If you are not satisfied with simple Toast view in Android, then you can go ahead to make a custom Toast. Actually, custom Toast is a modified simple Toast that makes your UI more attractive. So that when you create a custom Toast then two things are required, one is XML (custom_toast.xml) required for layout view of custom Toast and another is activity class (custom_activity.class) file where you can write Java code. Java class file passes the root View object to the setView(View) method.

custom_toast.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00AABB"
    android:orientation="horizontal"
    android:padding="8dp">
  
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_warning" />
  
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="8pt"
        android:textStyle="italic" />
</LinearLayout>

chevron_right


This is a XML layout for example purposes, so if you want your own design then you can create your own XML.

Custom_Activity.class

filter_none

edit
close

play_arrow

link
brightness_4
code

// get layout inflator object to inflate custom_toast layout
LayoutInflater inflater = getLayoutInflater();
  
// inflating custom_toast layout
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom_toast_container));
  
// Find TextView elements with help of layout object.
TextView text = (TextView)layout.findViewById(R.id.text);
  
// set custom Toast message.
text.setText("Custom Toast message");
  
// Create the Toast object
Toast toast = new Toast(getApplicationContext());
  
// to show toast at centre of screen
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  
// display duration then show() method displays Toast.
toast.setDuration(Toast.LENGTH_LONG);
  
// set custom layout 
toast.setView(layout);
  
// show toast
toast.show();

chevron_right




My Personal Notes arrow_drop_up

different thinking makes something new

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.