Open In App

Automatic Dialog Dismiss in Android

Last Updated : 18 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Imagine a scenario where a dialog appears, but the user doesn't interact with it. The dialog could potentially block other actions or disrupt the user experience. To address this issue, developers can employ a technique known as automating dialog dismissal using timers. In this article, we will explore how to set up a timer to automatically dismiss dialog boxes in Android applications. A sample video is given at the end to get an idea about what we are going to do in this article.

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.

Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to the file. In this code, we are going to implement a Button by clicking this button an Alert Dialog will pop up and Automatically dismiss in 5 seconds.

activity_main.xml:

XML
<?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:gravity="center"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Alert Box"/>

</LinearLayout>


Step 3: Working with the MainActivity.kt file

In this step, we are going to apply onClickListener to the button so that when the user clicks the button an alert dialog will open and automatically dismiss after 5 seconds. Below is the code for MainActivity.kt. Comments are added for a better understanding of the Code. The below function is Responsible for populating the alert dialog and dismiss it after 5 seconds.

MainActivity.kt:

Kotlin
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    private lateinit var alertDialog: AlertDialog
    private lateinit var button: Button
    private val dismissalDelayMillis = 5000L // 5 seconds

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

        // Initialize the button
        button = findViewById(R.id.button)

        // Set a click listener for the button
        button.setOnClickListener {
            openAlertDialog()
        }
    }

    // Function to open the AlertDialog and dismiss after 5 seconds
    private fun openAlertDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("Auto Dismiss Dialog")
        builder.setMessage("This dialog will automatically dismiss in 5 seconds.")
        builder.setPositiveButton("OK") { dialog, _ ->
            dialog.dismiss()
        }

        alertDialog = builder.create()
        alertDialog.show()

        // Use lifecycleScope to delay dismissal
        lifecycleScope.launch {
            delay(dismissalDelayMillis)
            if (!isFinishing && alertDialog.isShowing) {
                alertDialog.dismiss()
            }
        }
    }
}

Output:



Next Article

Similar Reads