This tutorial will be demonstrate the usage of Android Dialogs and associated event handling.
For more information on Android Dialog API, refer to Android API Reference
1. Things to know
A dialog is a visual component which is always attached to an Activity. Dialogs can be created from your Activity’s onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity. When a dialog is requested for the first time, onCreateDialog(int) instantiate the Dialog. After you create the Dialog, return the object at the end of the method. When you want to show a dialog, call showDialog(int) and pass it an integer that uniquely identifies the dialog that you want to display.
2. Creating the layout
In this example, I am creating a simple LinearLayout and a Button is attached to it. Click event on the button field is handled to display the Dialog.
File: main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/alertDialogBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="6.43" android:text="Alert Dialog" /> </LinearLayout>
2. Working with Android Activity class
File: DialogActivity.java
package com.objectechenica.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class DialogActivity extends Activity {
// Constant for identifying the dialog
private static final int DIALOG_ALERT = 10;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button alertDialog = (Button)findViewById(R.id.alertDialogBtn);
alertDialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_ALERT);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This will end the activity");
builder.setCancelable(true);
builder.setPositiveButton("I agree", new OkOnClickListener());
builder.setNegativeButton("No, no", new CancelOnClickListener());
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Activity will continue", Toast.LENGTH_LONG).show();
}
}
private final class OkOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "I was just kidding", Toast.LENGTH_LONG).show();
}
}
}
Related Posts:
- Android Radio Button Example
- Android ListView Tutorial
- How to generate .apk and install into android device
- Custom Screen Title in Android
- Download Image using AsyncTask in android
- Android Button Example
- Using External Fonts in Android View
- Android Toast Example
- How to get Key Hashes for android-facebook app
- How to display HTML in android TextView?
Tags: Android, Android UI, Code Sample





Hey very cool blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…I’m happy to find numerous useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . . linia p
Your post, Android Dialog Example | Java Tech IG, is really well written and insightful. Glad I found your website, warm regards from Thomas!