2

I am trying to create a custom alert dialog with rounded corners using a dialog fragment, and I read that this can be achieved setting the STYLE_NO_FRAME constant to the setStyle method of the DialogFragment. I did that in the onCreate method, but I get the above mentioned RunTimeException. Any ideas on what might be wrong?

Here's my code:

public static class AlertDialogFragment extends DialogFragment {

    private static final String DIALOG_NUMBER_KEY = "dialogNumber";
    private int dNumber;

    public static AlertDialogFragment newInstance(int dialogNumber) {
        AlertDialogFragment mDialogFragment = new AlertDialogFragment();

        // Supply dialogNumber input as an argument
        Bundle args = new Bundle();
        args.putInt(DIALOG_NUMBER_KEY, dialogNumber);
        mDialogFragment.setArguments(args);

        return mDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo);
    }

    // Build AlertDialog using AlertDialog.Builder
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        dNumber = getArguments().getInt(DIALOG_NUMBER_KEY);

        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();


        switch(dNumber) {
        case DELETE_DIALOG:
            adb.setView(inflater.inflate(R.layout.delete_record_dialog, null))
            .setCancelable(true)
            .create();
            AlertDialog customDeleteDialog = adb.show();

            Button okButton = (Button) customDeleteDialog.findViewById(R.id.ok);
            Button cancelButton = (Button) customDeleteDialog.findViewById(R.id.cancel);

            okButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // some code here
                }
            });

            cancelButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            return customDeleteDialog;
      }
Community
  • 1
  • 1
Nick
  • 966
  • 1
  • 10
  • 20

2 Answers2

0

The example you linked to shows that call happening in the newInstance() method, so it's called after the dialog is instantiated but before it is rendered:

public static AlertDialogFragment newInstance(int dialogNumber) {
    AlertDialogFragment mDialogFragment = new AlertDialogFragment();

    // Supply dialogNumber input as an argument
    Bundle args = new Bundle();
    args.putInt(DIALOG_NUMBER_KEY, dialogNumber);
    mDialogFragment.setArguments(args);
    mDialogFragment.setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo);

    return mDialogFragment;
}
kris larson
  • 30,387
  • 5
  • 62
  • 74
0

I think that setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo); should be added before the super.onCreate(savedInstanceState); in your onCreate method.

This should do the trick.

akalipetis
  • 938
  • 1
  • 7
  • 14
  • I see that you're creating a custom dialog, have you tried applying the `setStyle` method to the dialog and not the fragment? Sorry I missed that in the first fast read. – akalipetis Mar 31 '15 at 08:46
  • It may work with a dialog, but I preferred to use a dialog fragment instead. I found a not perfect workaround, setting a drawable with rounded corners for the parent's background, and padding to 2dp or 5dp – Nick Mar 31 '15 at 13:59