AlertDialog using Fragment

AlertDialog is a handy subclass of Dialog that displays a simple message, typically with “yes” (positive) or “no” (negative) options. However, the Dialog class has been deprecated to make way for DialogFragments. I made a simple class called AlertDialogFragment that implements an AlertDialog within a DialogFragment.

Note: This project uses the v4 support library to support devices running Platform 8 [Android 2.2, Froyo] and upwards.

The AlertDialogFragment communicates with the activity that invokes it through an interface called OnAlertDialogClickListener

import android.content.DialogInterface;

public interface OnAlertDialogClickListener {

	/**
	 * This method is invoked when the positive button is clicked
	 * @param dialog
	 */
	public void onPositiveClick(DialogInterface dialog, int id);
	
	/**
	 * This method is invoked when the negative button is clicked
	 * @param dialog
	 */
	public void onNegativeClick(DialogInterface dialog, int id);
	
	/**This method is invoked hen the neutral button is clicked
	 * @param dialog
	 */
	public void onNeutralClick(DialogInterface dialog, int id);
	
}

The AlertDialogFragment class is as follows

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class AlertDialogFragment extends DialogFragment {

	private OnAlertDialogClickListener mListener;
	private int mIndex;

	@Override
	public Dialog onCreateDialog(Bundle sis) {
		Bundle bundle = getArguments();
		String posText = bundle.getString("positive");
		String negText = bundle.getString("negative");
		String neuText = bundle.getString("neutral");
		String title = bundle.getString("title");
		String message = bundle.getString("message");
		mIndex = bundle.getInt("id");

		mListener = (OnAlertDialogClickListener) getActivity();
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		if(posText!=null)
		{
			builder.setPositiveButton(posText, new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					mListener.onPositiveClick(dialog, mIndex);
				}
			});
		}
		if(negText!=null)
		{
			builder.setNegativeButton(negText, new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					mListener.onNegativeClick(dialog, mIndex);

				}
			});
		}
		if(neuText!=null)
		{
			builder.setNeutralButton(neuText, new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					mListener.onNeutralClick(dialog, mIndex);
				}
			});
		}
		builder.setMessage(message);
		builder.setTitle(title);
		return builder.create();

	}
}

DemoActivity creates instances of AlertDialogFragment

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DemoActivity extends FragmentActivity implements OnAlertDialogClickListener{
	private int mIndex;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.demo);
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("title", "AlertDialog");
				bundle.putString("message", "This is an AlertDialog enclosed in a DialogFragment");
				bundle.putString("positive", "Okay");
				bundle.putString("negative", "Close");
				bundle.putInt("id", mIndex++);
				AlertDialogFragment adFragment = new AlertDialogFragment();
				adFragment.setArguments(bundle);
				adFragment.show(getSupportFragmentManager(), "dialog");
			}
		});

	}

	public void onPositiveClick(DialogInterface dialog, int id) {
		Toast.makeText(getApplicationContext(), "Positive button was clicked in Dialog id #"+id, Toast.LENGTH_SHORT).show();
	}

	public void onNegativeClick(DialogInterface dialog, int id) {
		Toast.makeText(getApplicationContext(), "Negative button was clicked in Dialog id #"+id, Toast.LENGTH_SHORT).show();

	}

	public void onNeutralClick(DialogInterface dialog, int id) {
		//Nothing to do
	}


}

demo.xml used by DemoActivity contains a single button

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/button"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_centerInParent="true"
	    android:text="Click here"/>
</RelativeLayout>

The entire project is available here

Advertisements

2 Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s