Advertisement

Android DialogFragment Tutorial

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

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

    http://exandroidstudio.blogspot.in/2015/02/android-basic-tutorial-get-current-date.html