Toast and custom toast

Toast and Custom Toast in Android with Example

In this tutorial, we will learn about Toast and Custom Toast in Android.

What is Toast?

Toast provides information about the process in a small pop-up. It only fills the space required by the message in Toast, and the current activity remains visible and interactive without any interruptions. After the timeout expires, the toast will be hidden automatically.

Note: In Android, when we need to notify the consumer of a process without waiting for consumer input, toast is used. A small pop-up window that shows the message, and automatically hidden when the time expires.

LENGTH LONG -> It is used to display toast for a long time. If we set this duration, the toast will display a longer time.

LENGTH_SHORT->It is used to display toast for a short time. When we set the duration, the toast will display a short period of time.

For Example: Toast.makeText(this,”Developers Dome”,Toast.LENGTH_SHORT).show()

Preview:

File: MainActivity.kt

package com.sagar.toast

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast

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

    fun ShowToast(view: View) {
        Toast.makeText(this,"Developers Dome",Toast.LENGTH_SHORT).show()

    }
}

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast in Android\n Developers Dome"
        android:textSize="30sp"

        android:fontFamily="serif-monospace"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textColor="@color/white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.13" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="135dp"
        android:text="Show Toast"
        android:textColor="@color/black"
        android:onClick="ShowToast"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Custom Toast Example

In this example, we display a button for a custom Toast and trigger a click event for it. Every time the user clicks the custom Toast button, a pop-up message will appear on the screen saying “Custom Toast in Android Developer Dome” and an image will be displayed on the screen.

To customize the toast, we first extract the layout inflator and then inflate the custom toast layout from the XML file. After that, we get the TextView and ImageView references of the advanced layout and set the text and image to TextView and ImageView. Finally, we create a toast, pass the expanded layout to the View() method, and then use the show() Toast method to display the toast.

Preview:

MainActivity.kt [Custom Toast Example]

package com.sagar.toast

import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    var customToast: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        customToast = findViewById<View>(R.id.customToast) as Button

        customToast!!.setOnClickListener {
            val inflater = layoutInflater
            val layout: View = inflater.inflate(
                R.layout.custom_toast,
                findViewById<LinearLayout>(R.id.custom_toast)
            )
            val toastTextView =
                layout.findViewById<View>(R.id.custom_toast_txt) as TextView
            val toastImageView =
                layout.findViewById<View>(R.id.custom_toast_img) as ImageView
            toastTextView.text = "Custom Toast In Android"
            toastImageView.setImageResource(R.drawable.ic_baseline_perm_scan_wifi_24)
            val toast = Toast(applicationContext)
            toast.duration = Toast.LENGTH_LONG
            toast.view = layout
            toast.show()
        }
    }
}

FIle: activity_main.xml [For Custom Toast]

<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"
    android:paddingLeft="30dp"
    android:paddingTop="30dp"
    android:paddingRight="30dp"
    android:paddingBottom="30dp"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/customToast"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Custom Toast"
        android:textColor="#fff"
        android:textSize="20sp" />

</RelativeLayout>

File: Custom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DD422929"
    android:orientation="horizontal"
    android:padding="10dp">
    <!-- ImageVView and TextView for custom Toast -->
    <ImageView
        android:id="@+id/custom_toast_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/custom_toast_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff" />
</LinearLayout>
  • We hope that this guide will assist you in quickly creating a simple WebView Android app. If you have any problems, please post them in the comments section and we will gladly assist you.

This Post Has One Comment

Leave a Reply