DialogFragment : Using AlertDialog with custom layout

I’m rewriting my application with Fragments API support. In the original application I have an AlertDialog that is created like this :

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.button_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ListView mListView = (ListView) view.findViewById(R.id.mblist);
            getString(R.string.okay), null);
    Cursor dcursor = propManager.propCursor();
    if (dcursor.moveToFirst()) {
        String[] from = { Constants.NAME_DET,
                Constants.VALUE_DET };
        int[] to = { R.id.mbname, R.id.mbvalue };
        SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this,
                R.layout.info_list_row, dcursor, from, to);
    final Dialog dialog = builder.create();

How can I display the same dialog via a DialogFragment? I have read the documentation but have trouble figuring out how to proceed with this.

  • Waiting for Target Device to Come Online
  • Android - How to detect when 3d party app is starting
  • OK button in “Create new Android Virtual Device (AVD)” dialog does not do anything
  • How can I change language of my application
  • How to draw circle around a pin using Google MAps API v2
  • How to Correctly Extend LinearLayout to Create a Custom View
  • Does the Google Maps Android API Utility Cluster Manager have a minimum number of markers before it will create a cluster?
  • Android Studio 1.0 failing build at dexDebug or dexRelease
  • Enable and disable a Broadcast Receiver
  • Parallax Effect in Android's ViewPager
  • Crop square image to circle - Programmatically
  • onTabSelected Selected Not Called
  • 2 Solutions collect form web for “DialogFragment : Using AlertDialog with custom layout”

    I’m surprised that nobody answered. This is the solution:

    public class PropDialogFragment extends DialogFragment {
        private PropDialogFragment() { /*empty*/ } 
        /** creates a new instance of PropDialogFragment */
        public static PropDialogFragment newInstance() {
            return new PropDialogFragment();
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            //getting proper access to LayoutInflater is the trick. getLayoutInflater is a                   //Function
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.my_dialog, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    getActivity().getString(R.string.okay), null);
            return builder.create();

    You can show the dialog using :

    private void showDialog() {
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
        // Create and show the dialog.
        DialogFragment newFragment = PropDialogFragment.newInstance();
        newFragment.show(ft, "dialog");

    Basically you can just show your Fragment like this:

    SherlockDialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");

    And this would be a simple TimePickerFragment:

    public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener{
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.