In this tutorial I am going to discuss you about how to create a custom toast in android application .

A toast is just a kind o operation that show a simple small popup to the user for better user experience . It is used for showing messages in current activity for short intervals of time and the current activity remain visible .

Toasts automatically disappear after a some time duration. The default Toast view contains a TextView for showing messages on it. We can also customize it with adding an image to it and changing size, colour, gravity of the message text.

1.Create a button to your main layout file :

Now we create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<?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="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Btn"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"/>
    
</LinearLayout>

2. Create a custom layout for Toast view :

Here we create an ImageView and TextView for custom Toast view.

toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/abc"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DAAA">
    <ImageView
        android:id="@+id/iview"
        android:src="@drawable/patientcare2"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"/>
</LinearLayout>

3. Now open your MainActivity.java and add following code :

Now we create a custom Toast view  .

package com.technxtcodelabs;;

import android.support.v7.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.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create a Custom Toast
                View view =  getLayoutInflater().inflate(R.layout.list_layout,
                        (ViewGroup) findViewById(R.id.abc));
                ImageView iv = (ImageView) view.findViewById(R.id.iview);

                TextView text = (TextView) view.findViewById(R.id.txt);
                text.setText("This is a custom toast");
                // Create an Instance of Toast
                Toast toast = new Toast(MainActivity.this);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(view);
                toast.show();

            }
        });

    }
}

Now run the project/app to test custom toast in android app through android device or any Emulator .

%d bloggers like this: