Android DialogFragment Tutorial

In this tutorial, you will learn how to implement a DialogFragment in your Android application. A DialogFragment is 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. Alternatively, you can create an entirely custom dialog, such as an AlertDialog, with its own content. We will create buttons that will show a DialogFragment and a custom Alert DialogFragment.

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project DialogFragmentTutorial.

Application Name : DialogFragmentTutorial

Project Name : DialogFragmentTutorial

Package Name : com.androidbegin.dialogfragmenttutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created two buttons that will show different dialog fragments. The first button will show a simple DialogFragment and another with a custom Alert DialogFragment.

Next, create an XML graphical layout for the MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

Next, create a fragment. Go to File > New > Class and name it DFragment.java. Select your package named com.androidbegin.dialogfragmenttutorial and click Finish.

Open your DFragment.java and paste the following code.

DFragment.java

Next, create an XML graphical layout for the DialogFragment. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file dialogfragment.xml and paste the following code.

dialogfragment.xml

Next, create another fragment. Go to File > New > Class and name it AlertDFragment.java. Select your package named com.androidbegin.dialogfragmenttutorial and click Finish.

Open your AlertDFragment.java and paste the following code.

AlertDFragment.java

In this fragment, we have created custom alert DialogFragment that consist of two buttons, a positive and negative. We have prepared a sample alert dialog icon for this tutorial. Insert your downloaded sample alert dialog icon into your res > drawable-hdpi.

Sample Alert Dialog Icon

Androidhappy (1.2 KiB, 436 downloads)

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

No changes needed for the AndroidManifest.xml.

AndroidManifest.xml

Output:

DialogFragment ScreenShots

Source Code

DialogFragmentTutorial (1.1 MiB, 529 downloads)
  • Memo

    yes its ok

    but the method ” getDialog()” where defined ?

  • Amit

    that’s good :)

    please also write a tutorial to allow user to select date & time using DialogFragment (using EditText OnClick..) in SherlockFragment… By default need to show current date and time in EditText(s).

    I am able to do this by extending my java classes using Activity or FragmentActivity, but not with SherlockFragment
    and as you always write code for latest in Android, so you are the only hope for me, its little bit urgent.

  • genericid

    Hello and thanks for this tutorial.
    I am new in Android development but I’d like to ask a couple of questions:

    What is the advantage of creating a DialogFragment with a layout file, over creating a DialogFragment which builds itself based on AlertDialog? Couldn’t you just use a plain DialogFrament and modify the xml to have all the buttons you want and then just implement listeners in DialogFragment?

    Also since you are building a DialogFragment to host an AlertDialog, why not just launch an AlertDialog in the first place from MainActivity?

    I apologize if my questions are based on wrong ideas, but I am still learning…
    Thanks.

    • Andy Mills

      I think it depends on what you’re trying to accomplish. At this point, I just want to display a messagebox with values I get from the system; I’m working on a camera app at the moment and want to be sure what values I’m picking up with unfamiliar functions. You’d think it’d be as simple as:
      AlertDialog ad = new AlertDialog(“title”, “message”, buttonConfig);
      ad.show();

      I will add more functionality (setting button text, adding negativeButton functionality), but it might help to see how I got this working. I basically copied this:
      stackoverflow.com/questions/18413925/how-can-i-reuse-an-alertdialog-for-yes-no-on-android#answer-26813455

      However, it’s not clear whether the MainActivity function ShowDialogFragment() is necessary. I was able to “get it to work” using the following:

      CustomDialog dialog2 = new CustomDialog() {
      @Override
      public void confirm() {
      //I’m not doing anything now, Just displaying the dialog…
      }
      };
      dialog2.setArgs(“display this text”);
      dialog2.show(getSupportFragmentManager(), “dialog”);

      Again, it might depend on what you’re doing. The ShowDialogFragment() function does appear to do some management, potentially reducing memory consumption. I haven’t dove too deep into it yet (again, dialogs are just giving me data at this point, I’m not actually using them to drive logic or user input (I’ll be moving into OpenGL next)

      Here’s an interesting read:
      http://stackoverflow.com/questions/13765127/dialogfragment-advantages-over-alertdialog/13765411#13765411