Android Development

How to Create Custom Toast Android with Example

In custom toast Android, You are able to design a toast message according to your choice. In this article, I just want to tell you how to create a custom toast on your android project. Creating a Simple Toast and Custom Toast is Different. You just need to know about some simple steps before creating a custom Toast. Let’s begin.

  1. First of all, you create a custom .xml file for toast, In this file you design your toast message you can append image, textview and other widget use to design your Custom Toast.
  2. In this example given below, I create a simple button when you tap on button short message appears on your screen. I add this button on activity_main.xml.
  3. On MainActivity.java file create a varible of button and find the button with the help of it’s id.
  4. And set onClickListner in Button.
  5. Using LayoutIflater initiate the custom toast xml file.
  6. After that set the duration, gravity and show the toast.

Create Custom Toast Android with Example

File: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/AndroidFreeks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:gravity="center_horizontal"
        android:text="@string/android_freeks"
        android:textColor="#4CAF50"
        android:textSize="28sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/AndroidFreeks"
        android:fontFamily="monospace"
        android:gravity="center_horizontal"
        android:text="@string/simple_toast_example"
        android:textColor="#F44336"
        android:textSize="45sp"
        android:textStyle="bold" />


    <Button
        android:id="@+id/toast_example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:backgroundTint="#4CAF50"
        android:text="@string/click_here" />


</RelativeLayout>

file: custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    app:cardCornerRadius="7dp"
    android:id="@+id/custom_toast_layout"
    app:cardUseCompatPadding="true"
    app:cardElevation="5dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="#C4CAEF9F">
        <ImageView
            android:contentDescription="@string/android_freeks"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_baseline_check_circle_24"
            android:layout_gravity="center_vertical"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center_vertical">
            <TextView
                android:id="@+id/success_notifications"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/success_notification"
                android:textSize="22sp"
                android:layout_marginLeft="12dp"
                android:textStyle="bold"
                android:textColor="#4CAF50"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="12dp" />
            <TextView
                android:id="@+id/success"
                android:layout_marginLeft="12dp"
                android:layout_width="wrap_content"
                android:textColor="#CD000000"
                android:layout_height="wrap_content"
                android:text="@string/description"
                android:layout_marginStart="12dp" />

        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

file: MainActivity.java

package com.example.toast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.toast_example);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = getLayoutInflater();
                View view = layoutInflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom_toast_layout));
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(view);
                toast.show();
            }
        });
    }
}

file: String.xml

<resources>
    <string name="app_name">Custom Toast</string>
    <string name="android_freeks">Android Freeks</string>
    <string name="simple_toast_example">Custom Toast Android</string>
    <string name="click_here">Click here!</string>
    <string name="success_notification">Success Notification</string>
    <string name="description">This is custom Notification Made by AndroidFreeks</string>
</resources>

Output:

custom toast android
naimishgupta

Share
Published by
naimishgupta

Recent Posts

How to use Toast on Android

Toast is a method, With the help of toast you can display a message that… Read More

2 months ago

How to use Loading Spinner in Android Studio

How to use Loading Spinner in Android Studio: If you want to learn to create… Read More

2 months ago

How To Update Drivers On Windows 10

Update Drivers On Windows 10 - For programs or hardware accessories to work properly with… Read More

3 months ago

Windows 11 release date official, specs, features, and all you need to know

Windows 11 release date- Windows 10 has been released for quite a long time now,… Read More

4 months ago

NVIDIA & Windows 10 (How I Solved the Code 43 Error)

The display of the error code 43 is often found in the Device status box… Read More

5 months ago

Forza Horizon 5 on Xbox Series and PC: Release Date, Location, & More

Forza Horizon is the outstanding companion to the mythical Xbox Forza racing franchise. Though the… Read More

6 months ago

This website uses cookies.