22

Ok I'm really confused here on how to make a custom dialog properly using the latest appcompat v23.0.1 Here is a couple of ways

First way:

public class AddTipDialogFrag extends DialogFragment
{
 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        int title = getArguments().getInt("title");
        AppCompatDialog dialogCompat = new AppCompatDialog(getActivity(), R.style.MyAlertDialogStyle);
        dialogCompat.setTitle(title); //doesn't work btw
        dialogCompat.setContentView(R.layout.add_tip_fragment);
        return dialogCompat;
    }
}
  

2nd way:

public class AddTipDialogFrag extends AppCompatDialogFragment
{ 
   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.add_tip_fragment,container, false);
        int title = getArguments().getInt("title");
        getDialog().setTitle(title);
        return view;
    }
}

Both ways seem to produce the same result. Personally I prefer the 1st way however there is an issue with ripple effect where it gets cut out when the custem view border ends, as you can see on the picture below.

ripple effect fail

Is this a bug? (It must be!) Can I fix it or should I just convert to the 2nd way? (Which works fine with the ripple effect). What is the best approach between those 2 ways considering most material dialog libraries are using the first way?

EDIT: the ripple glitch on the first way doesn't seem to occur anymore so I'm still not sure which is the right way of those two.

Community
  • 1
  • 1
ThanosFisherman
  • 5,626
  • 12
  • 38
  • 63
  • 1
    You're are using the app compat support libraries, and `AppCompatDialogFragment` gives you the desired behavior, so I think you have already answered your own question. – James McCracken Sep 21 '15 at 22:13
  • Yeah I guess `AppCompatDialogFragment` works as expected but I just wanted to know what is the best approach for dialogs like that. Most material dialog libraries use the first way. That's why I was kind of skeptical but thanks for your reply – ThanosFisherman Sep 22 '15 at 00:47
  • How to add positive and negative button in AppCompatDialogFragment? – AkshayT Nov 03 '17 at 17:25

2 Answers2

15

Second approch seems better as AppCompatDialogFragment extends DialogFragment and does all the heavy lifting for you. Also it looks cleaner with abstraction of unnecessary details (inline with OOP).

Also gives desired effact ;)

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
4

The Second Approach is better as because it is a special version of DialogFragment which uses an AppCompatDialog in place of a platform-styled dialog. The AppCompatDialogFragment is the sub class of DialogFragment and will inherit all the properties of the DialogFragment.

swetabh suman
  • 1,949
  • 2
  • 19
  • 24