Android Toast

Filed Under: Android

Toast message is useful to show notification for small time in android app. In this tutorial, we’ll discuss and implement android Toast message example.

Android Toast

Android Toast is used to show notification for a particular interval of time at the bottom of the screen by default. Toast message doesn’t block the user interaction and auto disappears after a timeout. The android.widget.Toast class is the subclass of java.lang.Object class.

Creating a Basic Toast message

Android Toast message is created using the method makeText() that is passed with the context, the message, and the duration as shown below:


Toast toast = Toast.makeText(context, "String goes here", duration);

The context can be of the application or the activity. It’s recommended to use getApplicationContext() to let the Toast be displayed irrespective of the current state of the Activity.

The duration can be set as Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

The Toast is displayed using the method show().


Toast.makeText(getApplicationContext(),"Basic Toast message", Toast.LENGTH_SHORT).show()

Positioning your Toast Message

A standard Toast message appears at the bottom of the screen. We can set our own gravity on a Toast as shown below


toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);

The second and third parameters are used to shift the toast to the right and down respectively by the offset specified.

Custom Layout for Toast Android

To create a custom layout we can define the view layout in the XML lets say custom_toast.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:background="#7DA1BC"
    >
    <ImageView android:src="@android:drawable/stat_notify_error"
        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_gravity="center_vertical"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        />
</LinearLayout>

In our activity class, we’ll inflate the above layout and set it on the Toast using the method setView().


LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
        (ViewGroup) findViewById(R.id.custom_toast_container));

TextView text = layout.findViewById(R.id.text);
text.setText("This is a custom toast");

toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Let’s create an application in which each button would display a different type of Toast message, amongst the ones we’ve just discussed.

Android Toast Message Example Project Structure

android toast example, toast android

Android Toast Message Code

The code for the activity_main.xml layout is given below.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    tools:context="com.journaldev.toasts.MainActivity">

    <Button
        android:id="@+id/btnBasicToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basic Toast"/>

    <Button
        android:id="@+id/btnGravityToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basic Toast With Gravity"/>

    <Button
        android:id="@+id/btnOffsetToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basic Toast With Gravity And Offset"/>

    <Button
        android:id="@+id/btnCustomToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Toast"/>

</LinearLayout>

The code for the MainActivity.java is given below


package com.journaldev.toasts;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnToast, btnGravityToast, btnOffsetToast, btnCustomToast;

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

        btnToast = findViewById(R.id.btnBasicToast);
        btnGravityToast = findViewById(R.id.btnGravityToast);
        btnOffsetToast = findViewById(R.id.btnOffsetToast);
        btnCustomToast = findViewById(R.id.btnCustomToast);
        btnToast.setOnClickListener(this);
        btnGravityToast.setOnClickListener(this);
        btnOffsetToast.setOnClickListener(this);
        btnCustomToast.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBasicToast:
                Toast.makeText(getApplicationContext(), "Basic Toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnGravityToast:
                Toast toast = Toast.makeText(getApplicationContext(), "Toast with Gravity", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                toast.show();
                break;
            case R.id.btnOffsetToast:
                toast = Toast.makeText(getApplicationContext(), "Toast With Offset", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 50, 50);
                toast.show();
                break;
            case R.id.btnCustomToast:
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom_toast,
                        (ViewGroup) findViewById(R.id.custom_toast_container));

                TextView text = layout.findViewById(R.id.text);
                text.setText("This is a custom toast");

                toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
                break;
        }
    }
}

The output of the android toast example application in action is given below.
android-toasts-output

Did you notice the position change between the Gravity Toast and the one with Offset included?

This brings an end to toast android tutorial. You can download the final Android Toast Example Project from the link below.

Reference: Official Doc.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages