Tutorials Android Android Dialog Fragment Example

Android Dialog Fragment Example

1. Introduction

The dialog is like any other window that pops up in front of the current window, used to show some short message, taking user input or to accept users decision. Unlike Toast, a dialog is generally used where user attention is mandated. Android supports several different ways to create a dialog such as AlertDialog and FragmentDialog. This example will widely cover all the aspect of DialogFragment.

Since the release of Android 3.0 (API level 11), the fragment can show as a dialog and call as DialogFragment. If you’re supporting older android versions, you can make use of fragment-compatibility support library.

To create a dialog fragment, we will be using android.app.DialogFragment class. This class is derived from the Fragment and behaves much like a fragment with all available fragment lifecycle methods.

In the following sections, you learn how to use dialog fragment to show a simple alert dialog from activity.

2. Creating dialog fragment

First step towards creating dialog fragment is to create a new class and extend it from DialogFragment. In the following code snippet, I am creating a new class named MyDialogFragment.

public class MyDialogFragment extends DialogFragment {


As a DialogFragment is much like any other fragment, the same lifecycle rules are applied. Now we have to override onCreateView or onCreateDialog method to provide the view hierarchy and construct the dialog fragment.

public class MyDialogFragment extends DialogFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_sample_dialog, container, false);
        getDialog().setTitle("Simple Dialog");
        return rootView;

In the above code, the onCreateView() method is expected to return the instance of view that represent the view hierarchy for your dialog. The instance of layout inflater can be used to inflate the layout for the fragment. ViewGroup represents the parent view where the fragment to be attached, and the Bundle param is the saved bundle data if available to restore the fragment to previous state.

3. Defining dialog fragment layout

Notice that, we are using fragment_sample_dialog layout form onCreateView() method, which is not declared anywhere. Let us create a simple layout that displays an ImageView and TextView as shown in the following screenshot.

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

        android:src="@drawable/image" />

        android:text="Lorem ipsum dolor sit amet..."
        android:textSize="20dp" />

        android:text="Dismiss" />

4. Displaying dialog fragment

We are pretty much done with creating dialog fragment. Put the following code snippet in Activity to show the dialog when user clicks on the button.

FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");

At this point we can run the application and it will produce the output as shown in the following screenshot.
Android DialogFragment Example

5. Dismiss dialog fragment

Dialog fragment can be dismissed by calling dismiss() method from MyDialogFragment class. Place the following code snippet in MyDialogFragment onCreateView() method before return statement to dismiss the dialog when user clicks on Dismiss button.

Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

The fragment dialog dismiss() method dismiss the dialog by removes the fragment from fragment manager, and then commit the fragment transaction.

6. Dialog Fragment using onCreateDialog()

There are two approaches to attach view group hierarchy to a dialog. Either we can override onCreateView() method and inflate layout as we have done in the step, or alternatively we can override onCreateDialog() method and supply a dialog instance. The following code hows how to create dialog fragment using onCreateDialog method.

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Simple Dialog");
    builder.setMessage("Some message here");

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

    return builder.create();
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
6 years ago

Good tutorial but its not detailed written . On wich button opet this dialog ?

Previous Post
Android Developer Days 2015

Android Developer Days 2015 Waits You

Next Post
Website Maintenance

Our Apologies for Server Downtime This Evening

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
Would love your thoughts, please comment.x