Correct way to make a custom material dialog with appCompat 23+

ThanosFisherman picture ThanosFisherman · Sep 17, 2015 · Viewed 9.4k times · Source

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.

Answer

Rahul Tiwari picture Rahul Tiwari · Sep 23, 2015

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