How to customize Toast in Android

In this android programming source code example, we are going to display and customize toast in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to display and customize toast in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity9">

    <Button
        android:id="@+id/showToast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Custom Toast"
        android:textColor="#ffffff"
        android:background="@color/colorAccent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

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

import androidx.appcompat.app.AppCompatActivity;

import com.bluapp.androidview2.R;

public class DeviceFeaturesActivity9 extends AppCompatActivity {
    private Button showToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features9);
        showToast = (Button) findViewById(R.id.showToast);
        showToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customToast();
            }
        });
    }

    private void customToast() {
        View toastView = getLayoutInflater().inflate(R.layout.custom_toastview, null);
        Toast toast = new Toast(DeviceFeaturesActivity9.this);
        toast.setView(toastView);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.show();

    }
}

custom_toastview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_toast_bg"
    android:padding="10dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom toast."
        android:textSize="20dp"
        android:layout_gravity="center_vertical"/>

</LinearLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.