Toast

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.

The Toast is shown at the bottom of the screen in the above screenshot (but you can change that).


Create Toast
Toast toast = Toast.makeText(this, "This is a message displayed in a Toast", Toast.LENGTH_SHORT);
toast.show();

Toast Position: You can change the positioning on the screen of a Toast message using the setGravity() method. Here is a Toast setGravity() example:

toast.setGravity(Gravity.CENTER, 0, 0);

In setGravity method IInd and IIIrd parameter use to set position, like if IInd parameter is 20 than toast will be 20 pixel from left and if IIIrd parameter is 20 than toast will display 20 pixel down from top.

And Ist parameter can by multiple with | operator

ToastExActivty.java
package com.theitbulls.dialogex;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ToastExActivty extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.toast_activity);
    }

    public void defaultToast(View view) {
        Toast.makeText(this, "This is default toast example", Toast.LENGTH_SHORT).show();
    }

    public void topToast(View view) {
        Toast toast = Toast.makeText(this, "Toast on top example", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
    }

    public void topLeftToast(View view) {
        Toast toast = Toast.makeText(this, "Toast on top left example", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
        toast.show();
    }

    public void centerToast(View view) {
        Toast toast = Toast.makeText(this, "Toast on center example", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}


Custom Toast: It is possible to define a custom View for your Toast. To do so, first you must create a layout XML file for the custom View. Here is an example Toast layout XML file:

toast_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/darker_gray">

    <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="10dp"
        android:src="@drawable/small_logo" />

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="10pt"
        android:textColor="@android:color/white"
        android:text="This is custom text example" />

</LinearLayout>

How to show custom toast:
public void customToast(View view) {
    LayoutInflater inflater = getLayoutInflater();
    View toastView = inflater.inflate(R.layout.custom_toast, null, false);

    Toast toast = Toast.makeText(this, "Custom Toast example", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setView(toastView);
    toast.show();
}