Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 667
ThanosFisherman Avatar asked Sep 17 '15 02:09

ThanosFisherman


2 Answers

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

like image 61
Rahul Tiwari Avatar answered Nov 16 '22 23:11

Rahul Tiwari


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.

like image 4
swetabh suman Avatar answered Nov 16 '22 23:11

swetabh suman