Android DialogFragment Tutorial

  • Memo

    yes its ok

    but the method ” getDialog()” where defined ?

    • Aritra

      it’s a FragmentDialog method.

  • 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…

    • 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);;

      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:

      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() {
      public void confirm() {
      //I’m not doing anything now, Just displaying the dialog…
      dialog2.setArgs(“display this text”);, “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:

  • Kousik K

    how to create dialog box like sticky?

  • Albert Jasiński

    Its great

  • surani saunak

    Fabulous tips thanks..,,,

    if you want to know more about android-basic-tutorial-get-current-date then goto this link.

  • Zaphod Beeblebroks

    Next step is how to theme properly DialogFragment

  • Jay

    Thanks…very helpful tutorial

  • Heena Pathan

    How to change margin of transparent background?