How do I display an alert dialog on Android

0 votes
I want to display a dialog window with a message to the user that shows "Are you sure you want to delete this entry?" with one button that says 'Delete'. When Delete is touched, it should delete that entry, otherwise nothing. I have written a click listener for those buttons, but how do I invoke a dialog or popup and its functionality?
Feb 22, 2022 in Others by Soham
• 9,700 points
1,163 views

1 answer to this question.

0 votes

You could use an AlertDialog for this and construct one using its Builder class. The example below uses the default constructor that only takes in a Context since the dialog will inherit the proper theme from the Context you pass in. However, there is a constructor that allows you to specify a specific theme resource as the second parameter if you desire to do so.

new AlertDialog.Builder(context) 
        .setTitle("Delete entry") 
        .setMessage("Are you sure you want to delete this entry?") 

        // Specifying a listener allows you to take an action before dismissing the dialog. 
        // The dialog is automatically dismissed when a dialog button is clicked.   .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
                // Continue with delete operation 
    } 
}) 

// A null listener allows the button to dismiss the dialog and take no further action. .setNegativeButton(android.R.string.no, null) 
.setIcon(android.R.drawable.ic_dialog_alert) 
.show();
answered Feb 22, 2022 by Aditya
• 7,680 points

Related Questions In Others

0 votes
1 answer

How do I install Ruby on Rails?

Hi @Anvi, what @Pratibha has mentioned is ...READ MORE

answered Mar 1, 2019 in Others by Abha
• 28,140 points
824 views
0 votes
0 answers

How do I get data storage used on my AWS RDS?

I wish to know how much storage ...READ MORE

Apr 9, 2022 in Others by Kichu
• 19,050 points
247 views
0 votes
1 answer

How do I protect all worksheet in an Excel workbook with a single click?

VBA Code : Dim ws as Worksheet Dim pwd ...READ MORE

answered Oct 22, 2022 in Others by narikkadan
• 63,420 points
515 views
0 votes
1 answer

How do I get an Excel range using row and column numbers in VSTO / C#?

Use: int countRows = xlWorkSheetData.UsedRange.Rows.Count; int countColumns = xlWorkSheetData.UsedRange.Columns.Count; object[,] ...READ MORE

answered Nov 17, 2022 in Others by narikkadan
• 63,420 points
1,265 views
0 votes
1 answer
0 votes
1 answer

How to create a Custom Dialog box in android?

Here I have created a simple Dialog, ...READ MORE

answered Feb 18, 2022 in Others by Rahul
• 9,670 points
675 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,360 points
3,367 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,090 points
2,163 views
0 votes
1 answer

How do I get the current date and time in PHP?

The time would go by your server ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
512 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP