I want to display a dialog/popup 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?
Just a simple one! Create a dialog method, something like this anywhere in your Java class:
Now create Layout XML
dialog_demo.xml
and create your UI/design. Here is a sample one I created for demo purposes:Now you can call
openDialog()
from anywhere you like :) Here is the screenshot of above code.Note that text and color are used from
strings.xml
andcolors.xml
. You can define your own.I'd like to add on David Hedlund great answer by sharing a more dynamic method than what he posted so it can be used when you do have a negative action to perform and when you don't, i hope it helps.
You can create Activity and extends AppCompatActivity. Then in the Manifest put next style:
Inflate it by Buttons and TextViews
Then use this like a dialog.
For example, in the linearLayout I fill next parameters:
for me
Use AlertDialog.Builder
You will get the following output.
To view alert dialog tutorial use the link below.
Android Alert Dialog Tutorial
The code which David Hedlund has posted gave me the error:
If you are getting the same error use the below code. It works!!