Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

8. Dialogs via the AlertDialog

We have already used a "Toast" which is a small message window which does not take the focus. In this chapter we will use the class "AlertDialog". AlertDialog is used to open a dialog from our activity. This modal dialog gets the focus until the user closes it.

An instance of this class can be created by the builder pattern, e.g. you can chain your method calls.

You should always open a dialog from the class onCreateDialog(int) as the Android system manages the dialog in this case for you. This method is automatically called by Android if you call showDialog(int).

Create a new Android project "de.vogella.android.alertdialog" with the activity "ShowMyDialog". Maintain the following layout for "main.xml".

			
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<Button android:id="@+id/Button01" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Show Simple Dialog"
		android:onClick="openMyDialog"></Button>
</LinearLayout>

		

Change the code of your activity to the following.

			
package de.vogella.android.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class ShowMyDialog extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	}

	public void openMyDialog(View view) {
		showDialog(10);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 10:
            // Create out AlterDialog
            Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("This will end the activity");
            builder.setCancelable(true);
            builder.setPositiveButton("I agree", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ShowMyDialog.this.finish();
                }
            });
            builder.setNegativeButton("No, no", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        }
        return super.onCreateDialog(id);
    }

}
		

If you run your application and click your button you should see your dialog.

More on dialogs can be found on Android Dialogs standard documentation.