android confirm dialog

Android confirm dialog box with OK and Cancel button

This will probably be one of the Android code snippets that I will need to go back to from time to time.

I’m sharing the codes we need to show an Android confirm dialog to prompt the user to click “yes” or “no”, or in my case, “OK” or “Cancel”.

The following code for Android confirm dialog is in my MainActivity.java file:

Code snippet for “Android confirm dialog”

Run this on your Android phone or emulator. When you click OK or Cancel, you’ll see logs on Android Studio’s logcat “OK” or “Cancel”, whichever button you click.

The above code was adapted from (Android) Simple confirm dialog at Jymden.com, and then modified to work on my MainActivity.java.

By the way, I use Android Studio version 1.4 on Windows 7.

Catzie

A Filipino who is a mobile & web developer on weekdays, and a baker on weekends. When I have time to spare, I blog, play video games, watch series (usually anime), cook dishes, draw, or spend a little time with pets. I also like traveling and recording song covers, but I only do so occasionally. Follow on Twitter: @catzie

Leave a Reply

Your email address will not be published. Required fields are marked *