Thursday, 18 July 2013

Android Dialog Example

1. Android Dialog – 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>

3. Working with Android 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;

/** 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();
}
}

}