This tutorial explains Android Dialog Example and associated event handling.

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.

  1. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity.
  2. When a dialog is requested for the first time, onCreateDialog(int) instantiate the Dialog.
  3. 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.

Android Dialog Example

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.

<LinearLayout xmlns:android=""
   android:orientation="vertical" >
      android:text="Alert Dialog" />

Using Dialog from Activity class


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;

    public void onCreate(Bundle savedInstanceState) {

        Button alertDialog = (Button)findViewById(;
        alertDialog.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_ALERT:
                Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("This will end the activity");
                builder.setPositiveButton("I agree", new OkOnClickListener());
                builder.setNegativeButton("No, no", new CancelOnClickListener());
                AlertDialog dialog = builder.create();
        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(), "Just kidding", Toast.LENGTH_LONG).show();


Run the application and it results screen below

This article is about: Android UI, Code Sample,


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (, DZone MVB and founder of


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • MontanaJoe

    Great example, but it isn’t modal. How do you make it modal?

  • linia

    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

  • Thomas

    Your post, Android Dialog Example | Java Tech IG, is really well written and insightful. Glad I found your website, warm regards from Thomas!