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, 301 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, 326 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.