Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Toasts for Android Studio

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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





// 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;

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 : 


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

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 


<LinearLayout xmlns:android=""
        android:src="@drawable/ic_warning" />
        android:textStyle="italic" />

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 


package com.example.hp.splashscreen;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
// 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(;
// Find TextView elements with help of layout object.
TextView text = (TextView)layout.findViewById(;
// set custom Toast message.
text.setText("Custom Toast message");
// Create the Toast object
Toast toast = new Toast(getApplicationContext());
// to show toast at center of screen
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
// set display duration, then show() method displays Toast.
// set custom layout
// show toast;

My Personal Notes arrow_drop_up
Last Updated : 19 Jan, 2023
Like Article
Save Article
Similar Reads