Toasts for Android Studio
Last Updated :
31 Jan, 2025
A toast provides a simple popup message that is displayed on the current activity UI screen (e.g. Main Activity).
Example:
Syntax:
// 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();
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 :
Toast.makeText(MainActivity.this, "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.java) file where you can write Java code. Java class file passes the root View object to the setView(View) method.
activity_main.xml: (Creating custom Toast in main activity file)
activity_main.xml
<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" />
<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>
This is a XML layout for example purposes, so if you want your own design then you can create your own XML.
MainActitvity.java:
MainActivity.java
package com.gfg.myapplication;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get LayoutInflater object to
// inflate custom_toast layout
LayoutInflater inflater = getLayoutInflater();
// Inflating custom_toast layout
View layout = inflater.inflate(R.layout.activity_main,
(ViewGroup) findViewById(R.id.custom_toast_container));
// Find TextView element with
// the help of the layout object
TextView text = 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 the center
// of the screen
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
// Set display duration and then
// show() method to display Toast
toast.setDuration(Toast.LENGTH_LONG);
// Set custom layout
toast.setView(layout);
// Show toast
toast.show();
}
}
Output of the above Application:
Toast appears and disappears.