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 in Others by Soham
• 8,730 points
59 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 by Aditya
• 7,280 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,100 points
265 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 in Others by Kichu
• 16,250 points
19 views
0 votes
1 answer

How do I carry out functional testing?

Well, functional testing is pretty simple. it ...READ MORE

answered Feb 18, 2019 in Others by Jobin
169 views
0 votes
1 answer

How to do Installation of Ruby on Rails on Linux (Ubuntu)?

Hi Pratibha, installing ROR on ubuntu is ...READ MORE

answered Mar 1, 2019 in Others by Anvi
• 14,170 points
803 views
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 in Others by Rahul
• 8,980 points
26 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,350 points
2,940 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,060 points
1,471 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 in Others by Aditya
• 7,280 points
17 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP