Wolin Labs :: fun toys for your iThingy & Droid

Articles

Don't Lose your Garmin GPS!

Cron Job with Random Start Delay

Git Submodule with Local Changes Example

Using Caltopo Maps on your Garmin GPS

Waffle Reuben

Copying a Single File with a Yocto Recipe

Semihosting Debugging on STM32F4

Realtime Audio DSP with the STM32F4

Tuning the Moog Etherwave Theremin

STM32 Discovery Development on Linux

STM32F4 USB Virtual COM Port (VCP)

Editing GoPro Hero2 Video with Cinelerra

Code Browsing with Emacs, GLOBAL, and Speedbar

Use a PC Power Supply as a Bench Supply the Easy Way

Android Activity Bar Framework

Generating an Audio Sine Wave with Java

Android Simple Yes/No MessageBox

Fox Talas 32 Oil/Seal Change Checklist

Creating a Bootloader Environment (Freescale ColdFire Example)

Pentomino Smackdown: Names That Didn't Make the Cut

Android Simple Yes/No MessageBox


In Windows, you can ask a simple Yes/No question with one line:

if (IDYES == MessageBox(hWnd, "Are you sure?", "Erase hard drive", MB_YESNO|MB_ICONEXCLAMATION)) {
	EraseHardDrive();
}

I've seen a lot of posts asking how to do something similar in Android. I've also seen a lot of answers, but none I thought were clear and concise... I have combined the information I gleaned from various postings in an attempt to provide "one stop shopping" with a simple example.


This is a simple app that puts up a Yes/No message box and processes the input.
When the Yes button is pressed, it flashes a toast message.
When the No button is pressed, it does nothing.


public class YesNoSampleActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Put up the Yes/No message box
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder
    	.setTitle("Erase hard drive")
    	.setMessage("Are you sure?")
    	.setIcon(android.R.drawable.ic_dialog_alert)
    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int which) {			      	
    	    	//Yes button clicked, do something
    	    	Toast.makeText(YesNoSampleActivity.this, "Yes button pressed", 
                               Toast.LENGTH_SHORT).show();
    	    }
    	})
    	.setNegativeButton("No", null)						//Do nothing on no
    	.show();
        
    	    	
    	// Continue code after the Yes/No dialog
    	// ....
    }
}

Note: within the embedded anonymous DialogInterface.OnClickListener's onClick method, if you wish to access the YesNoSampleActivity object's this, you use the special syntax: YesNoSampleActivity.this

Download the complete Eclipse project source.



If you enjoyed this article, please consider buying my products ...

ATX PS Adapter Ultimate Serial Port
ATX PS Adapter

Use an ATX PC power supply as a 5V, 3.3V, and +12V/-12V bench supply the easy way, without cutting the case or mounting external connectors, resistors, LEDs, switches, and fuses.

Provides visual indication when supply is plugged in and turned on, also fuses the power voltage outputs for safety. Run USB powered development boards via the USB connectors on the 5V line.

Ultimate Serial Port (Debug Buddy)

USB serial port with standard, 5V and 3V RS232, plus integrated null modem and gender changer. Implements TX/RX and RTS#/CTS# for optional hardware handshake.

Also includes 3.3V<->5V level shifters, debug LEDs, and 13 clock sources. Valuable tool for hands on problem solving and hacking