Open In App

Display Toast For a Specific Time in Android

Last Updated : 06 Feb, 2022
Like Article

A Toast in Android is a message that appears on the screen for a specific time whenever invoked. This message appears at the bottom of the application leaving some margin at the bottom. In general, a Toast can be displayed for either 2 seconds (Toast.LENGTH_SHORT) or 3.5 seconds (Toast.LENGTH_LONG).

Toast in Android

In this article, we will show you how you could display Toast for longer or shorter in Android. Follow the below steps once the IDE is ready.

Understanding the concept

Actually, it is impossible to change these durations. However, we will use the existing duration Toast.LENGTH_LONG for displaying the Toast. Using this, a Toast can be displayed for 3.5 seconds. In runtime, we will call a Toast for 3.5 seconds and cancel it at the 2nd second. We shall keep repeating this for a specific time defined inside the main code. Additionally, we shall display a counter to check if Toast runs for a specific period. As the concept is now established, start developing the application with the steps below.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView to display the counter and a button to invoke both the counter and the Toast.


<?xml version="1.0" encoding="utf-8"?>

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package org.geeksforgeeks.toastdisplaylength
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    // Duration of the toast 
    // is 10 seconds (10000 milli-seconds)
    private val mToastDuration = 10000
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing 
        // the elements from the layout file
        val mTextView = findViewById<TextView>(
        val mButton = findViewById<Button>(
        // Creating a toast
        val mToast = Toast.makeText(applicationContext, "Sample Toast", Toast.LENGTH_LONG)
        // When button is clicked, counter starts
        // and toast is called
        mButton.setOnClickListener {
    // Function to display counter
    private fun mDisplayTimer(view: TextView){
        Thread {
            val mToastDurationSecs = mToastDuration/1000
            for (i in 1..mToastDurationSecs) {
                runOnUiThread {
                    view.text = i.toString()
    // Function to invoke Toast
    private fun mDisplayToast(toast: Toast){
            for(i in 1..mToastDuration/2000){


You can see that Toast appears for a specific period as counter runs.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads