July 21, 2020

How to display Toast notification in android

By Mohit Agrawal

Introduction

Hey guys, this is going to be a very basic android tutorial for the beginners especially. Today, I will show you how to display a toast notification in android.

I will design a simple android app that will have one button. And on the click of that button, I will show Toast Notification.

android toast 1

What is Toast notification in android?

android toast 2

Toast is a small pop up that is generally used to provide some general feedback about an event. Toasts automatically disappear after a timeout. It is not user interactive.

Implementation

1 STEP: Create a new android project and name it “Toast App”.

2 STEP: Open the activity_main.xml and paste the below code for the UI.

<?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=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="159dp"
        android:layout_marginLeft="159dp"
        android:layout_marginTop="339dp"
        android:layout_marginEnd="164dp"
        android:layout_marginRight="164dp"
        android:layout_marginBottom="344dp"
        android:text="Show Toast"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

3 STEP: Now open the MainActivity.kt and make a click listener for the button which we have created.

To display the toast we need to call Toast() method as shown below.

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            Toast.makeText(this, "Hello, This is a toast", Toast.LENGTH_LONG).show()
        }
    }
}

Now you can run the project and click on the button. You will see the Toast message.

If you are facing any problem then please let me know it in the comment section.

Toast notification FAQS

  1. When to use Toast notification?

    It is used when you have to inform the user immediately after some event. It is used to provide visual feedback for any event.
    Let's take an example to understand this. Suppose when the user deletes an item from a list then you can display a toast message that “Item deleted”.android toast 2

  2. Toast notification is not showing

    Sometimes you write the code for Toast message correctly but you forget to call the .show() method. That is the reason your toast message is not showing up.android toast 2

  3. Toast message vs Snack bar

    Toast is a small pop up that is generally used to provide some general feedback about an event. Toasts automatically disappear after a timeout. It is not user interactive.
    Suppose when the user deletes an item from a list then you can display a toast message that “Item deleted”.
    SnackBar is used where a simple popup message needs to be displayed along with an option to perform some action.
    Suppose when the user deletes an item then a quick SnackBar display at the bottom with Message ‘1 item deleted’ with an action button ‘Undo’. On pressing the ‘Undo’ action button, the deleted item will be restored.android toast 2

  4. Toast vs pop up

    The main difference Toast and pop up is toast is not meant for user interaction. It just comes for very short period of times and then disappears. But pop up contains the clickable buttons. User can interact with the pop up kind of notifications.android toast 2

  5. Toast notification display time

    By default, android gives us two time duration for displaying the toast message. The first one is Toast.LENGTH_LONG and another one is Toast.LENGTH_SHORT.android toast 2

Recommend post: Custom Toast Notification in Android

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!