AlertDialog sample program in Android
By: Ashley
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>
Archived Comments
1. Thanks for the simple and clean post Man!! Even this <a href="http://www.compiletimeerror.co
View Tutorial By: Meghna at 2013-08-03 17:40:25
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Get Location of an android phone programmatically
Solution to error: unable to open connection to server due to security error
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial
Intent in Android to call one activity from another activity.
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Reading a file sample program in Android
Date and Time sample program in Android
Gallery sample program in Android
GridView sample program in Android