StackTips

Android Dialog Example

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="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>

Using Dialog from Activity class

File: DialogActivity.java

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;

    @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(), "Just kidding", Toast.LENGTH_LONG).show();
        }
    }
}

Screenshot

Run the application and it results screen below

nilanchala avtar

Nilanchala Panigrahy

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 (goo.gl/qUZ0XV3)