Using DialogFragments in Android – A Simple example.

By | February 14, 2013

A little on Dialog Fragments

A fragment that displays a dialog window, floating on top of its activity’s window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment’s state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

Implementations should override this class and implement onCreateView(LayoutInflater, ViewGroup, Bundle) to supply the content of the dialog. Alternatively, they can override onCreateDialog(Bundle) to create an entirely custom dialog, such as an AlertDialog, with its own content.

Dialog Fragment

There are two ways you can create a DialogFragment.

1. By overriding onCreateView function
2. By overriding onCreateDialog function

Creating a DialogFragment using onCreateView

package com.coderzheaven.dialogfragmentdemo;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TestDialog extends DialogFragment {


    public TestDialog() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().setTitle("DialogFragment Demo");
        View view = inflater.inflate(R.layout.dialog_fragment, container);

        return view;
    }
}

Now the Activity that uses this fragment.

package com.example.dialogfragments;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		FragmentManager fm = getFragmentManager();
		TestDialog testDialog = new TestDialog();
		testDialog.setRetainInstance(true);
		testDialog.show(fm, "fragment_name");

	}

}

Creating a Dialog Fragment using onCreateDialog.

This is another method of creating a Dialog Fragment.
Replace the onCreateView with the following method

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
	AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	builder.setTitle("Dialog Fragment");
	builder.setMessage("This is a simple Dialog Fragment.");

	builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dismiss();
		}
	});

	builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dismiss();
		}
	});

	return builder.create();
}

Dismissing a Dialog Fragment

You can dismiss a DialogFragment by simply calling “dismiss()”.

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	@Override
	public void onClick(DialogInterface dialog, int which) {
		dismiss();
	}
});

Below are the layout for the Dialogs mentioned above.

dialog_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lbl_your_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello from CoderzHeaven, DialogFragment Demo" />

</LinearLayout>

You can download the complete Android Studio Source Code from here.

Please send your valuable comments to coderzheaven@gmail.com.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *