Open In App

Display Toast For a Specific Time in Android

Last Updated : 06 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/text_view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100sp"
        android:textSize="50sp"/>
  
    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"
        android:layout_centerInParent="true"/>
  
</RelativeLayout>


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.

Kotlin




package org.geeksforgeeks.toastdisplaylength
  
import androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing 
        // the elements from the layout file
        val mTextView = findViewById<TextView>(R.id.text_view_1)
        val mButton = findViewById<Button>(R.id.button_1)
  
        // 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 {
            mDisplayTimer(mTextView)
            mDisplayToast(mToast)
        }
    }
  
    // Function to display counter
    private fun mDisplayTimer(view: TextView){
        Thread {
            val mToastDurationSecs = mToastDuration/1000
            for (i in 1..mToastDurationSecs) {
                runOnUiThread {
                    view.text = i.toString()
                }
                Thread.sleep(1000)
            }
        }.start()
    }
  
    // Function to invoke Toast
    private fun mDisplayToast(toast: Toast){
        Thread{
            for(i in 1..mToastDuration/2000){
                toast.show()
                Thread.sleep(2000)
                toast.cancel()
            }
        }.start()
  
    }
}


Output:

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads