Programming Tutorials

AlertDialog sample program in Android

By: Ashley in Android Tutorials on 2011-07-02  

This sample android program shows you how to use an Alert Dialog box in Android.

The AlertDialogDemo.java file is as follows:

package com.javasamples;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AlertDialogDemo extends Activity {
	Button btnGo;
	EditText txtMsg;
	String msg;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtMsg =  (EditText)findViewById(R.id.txtMsg);
        
        btnGo = (Button) findViewById(R.id.btnGo);
        btnGo.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				AlertDialog diaBox = makeAndShowDialogBox();
								
			    diaBox.show();
			    
				txtMsg.setText("I am here!");
			}
        	
        });
    }//onCreate
    



	private AlertDialog makeAndShowDialogBox(){
    	
        AlertDialog myQuittingDialogBox = 

        	new AlertDialog.Builder(this) 
        	//set message, title, and icon
        	.setTitle("Terminator") 
        	.setMessage("Are you sure that you want to quit?") 
        	.setIcon(R.drawable.ic_menu_end_conversation)
        	
        	//set three option buttons
        	.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        		public void onClick(DialogInterface dialog, int whichButton) { 
            	 //whatever should be done when answering "YES" goes here
        		 msg = "YES " + Integer.toString(whichButton);
        		 txtMsg.setText(msg);
        		}              
        	})//setPositiveButton
        /*	.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { 
        		public void onClick(DialogInterface dialog, int whichButton) { 
            	 //whatever should be done when answering "NO" goes here
        		 msg = "Cancel " + Integer.toString(whichButton);	
        		 txtMsg.setText(msg);
        		}
        		})*/


        	.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
        		public void onClick(DialogInterface dialog, int whichButton) { 
            	 //whatever should be done when answering "NO" goes here
        		 msg = "NO " + Integer.toString(whichButton);	
        		 txtMsg.setText(msg);
             } 
        	})//setNegativeButton
        	
        	.create();
        	
        	return myQuittingDialogBox;
    }
    
}//AndSelectionWidgets

The output is as shown in the emulator below

The main.xml file in your res/layout folder is as follows:

<LinearLayout 
    android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android" 
	android:orientation="horizontal">
	<Button 
	   android:text="GO" 
	   android:id="@+id/btnGo" 
	   android:layout_width="wrap_content" 
	   android:layout_height="wrap_content">
	 </Button>
	 <EditText 
	   android:hint="click the button" 
	   android:id="@+id/txtMsg"
	   android:layout_width="fill_parent" 
	   android:layout_height="wrap_content">
	 </EditText>

</LinearLayout>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Android )

AlertDialog sample program in Android

Getting Started with Android

adb.exe: no devices/emulators found

Is it safe to delete userdata-qemu.img userdata-qemu.img.qcow2 files

Android Emulator

Progress bar and downloading a file sample program in Android

Gallery sample program in Android

ArrayAdapter sample program in Android

compileSdkVersion vs buildToolsVersion in app/build.gradle

Gradle, npm, react-native - How are they related?

Emulator: glTexImage2D: got err pre :( 0x506 internal 0x8058 format 0x1908 type 0x1401

gradle build failed Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

Emulator: WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***

./gradlew assembleDebug '.' is not recognized as an internal or external command, operable program or batch file.

'adb' is not recognized as an internal or external command, operable program or batch file.

Latest Articles (in Android)

Keep your android phone awake while debugging

compileSdkVersion vs buildToolsVersion in app/build.gradle

gradle build failed Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

Gradle, npm, react-native - How are they related?

Emulator: glTexImage2D: got err pre :( 0x506 internal 0x8058 format 0x1908 type 0x1401

Emulator: WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***

./gradlew assembleDebug '.' is not recognized as an internal or external command, operable program or batch file.

'adb' is not recognized as an internal or external command, operable program or batch file.

Performing Streamed Install adb: failed to install app buildoutputsapkdebugapp-debug.apk: Exception occurred while executing 'install': android.os.ParcelableException: java.io.IOException: Requested internal only, but not enough space

Is it safe to delete userdata-qemu.img userdata-qemu.img.qcow2 files

adb.exe: no devices/emulators found

How to start the Android emulator

Get Location of an android phone programmatically

Getting Started with Android

Solution to error: unable to open connection to server due to security error

Related Tutorials

Keep your android phone awake while debugging

compileSdkVersion vs buildToolsVersion in app/build.gradle

gradle build failed Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

Gradle, npm, react-native - How are they related?

Emulator: glTexImage2D: got err pre :( 0x506 internal 0x8058 format 0x1908 type 0x1401

Emulator: WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***

./gradlew assembleDebug '.' is not recognized as an internal or external command, operable program or batch file.

'adb' is not recognized as an internal or external command, operable program or batch file.

Performing Streamed Install adb: failed to install app buildoutputsapkdebugapp-debug.apk: Exception occurred while executing 'install': android.os.ParcelableException: java.io.IOException: Requested internal only, but not enough space

Is it safe to delete userdata-qemu.img userdata-qemu.img.qcow2 files

adb.exe: no devices/emulators found

How to start the Android emulator

Get Location of an android phone programmatically

Getting Started with Android

Solution to error: unable to open connection to server due to security error