Thursday, October 24, 2013

Android Dialogs

Using dialogs in Android
This tutorial describes how to use Android dialogs. It is based on Eclipse 4.2, 1.6 and Android 4.2.

1. Android Dialogs

1.1. Overview

You can open dialogs from your activity via the showDialog(int) method. Dialogs are always created and are part of an activity. A dialog gets the focus until the user closes it.
Dialog is the base class for dialogs, typically you use on of its subclasseds, e.g. AlertDialog,ProgressDialogDatePickerDialog or TimePickerDialog.
// Constant for identifying the dialog
private static final int DIALOG_ALERT = 10;

public void onClick(View view) {
showDialog(DIALOG_ALERT);
}
If the dialog is displayed the first time Android calls the onCreateDialog(int) method. In this method you instantiate the correct dialog based on the input parameter. You should always create a dialog from the onCreateDialog(int) method as the Android system manages in this case the dialog for you.
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
// create out AlterDialog
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) {
AlertExampleActivity.this.finish();
}
}
onCreateDialog(int) is only called the first time, if you want to later influence the dialog use the optional onPrepareDialog(int, Dialog) method.

1.2. ProgressDialog

Android provides also a ProgressDialog, which can be opened via a ProgressDialog.open()method call.

1.3. Own Dialogs

If you want to create your own dialogs, you create a layout file for the dialog. This layout file is assigned to the dialog via the setContentView() method.
You would then use the dialog.findViewById() to find the elements in your layout and assign values to it.
The title of the dialog can be set via the setTitle() method.

2. Tutorial: Alert Dialog

The following will give an example of using the AlertDialog dialog An instance of this class can be created by the builder pattern, e.g. you can chain your method calls.
Create a new Android project named "de.vogella.android.dialog.alert" with the activity calledAlertExampleActivity. 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button" />

</LinearLayout>
Change the code of your activity to the following.
package de.vogella.android.dialog.alert;

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 AlertExampleActivity extends Activity {

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);

}

public void onClick(View view) {
showDialog(DIALOG_ALERT);
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
// create out AlterDialog
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) {
AlertExampleActivity.this.finish();
}
}

}
If you run your application and click your button you should see your dialog.
Showing the running application with the dialog open

No comments:

Post a Comment