mattfred
mattfred

Reputation: 2759

Style DialogFragment Dynamically

I currently style the title section of an AlertDialog dynamically. However, I can only do this after the dialog is shown. I would like to move all my dialogs to their own class and extend DialogFragment, as Google shows here.

So currently after creating an AlertDialog I would do something like this:

AlertDialog dialog = builder.create();
dialog.show();
colorAlertDialogTitle(dialog);

public static void colorAlertDialogTitle(AlertDialog dialog, ThemeColors colors) {
    int color = myColor;
    int background = myBackgroundColor;

    int dividerId = dialog.getContext().getResources().getIdentifier("titleDivider", "id", "android");
    ImageView divider = (ImageView) dialog.findViewById(dividerId);
    if (divider != null) {
        divider.setBackgroundColor(color);
    }

So now, when I move my code to a class that extends DialogFragment, and I try to style the dialog in that class before the dialog is shown, the app crashes with this error:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:317)
    at com.android.internal.app.AlertController.installContent(AlertController.java:231)
    at android.app.AlertDialog.onCreate(AlertDialog.java:423)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
    at android.app.Dialog.show(Dialog.java:295)
    at android.app.DialogFragment.onStart(DialogFragment.java:499)
    at android.app.Fragment.performStart(Fragment.java:2244)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
    at android.app.BackStackRecord.run(BackStackRecord.java:793)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

So I'm at a loss as to how to use the correct way of creating dialogs and still be able to style them dynamically.

Once I move it to a DialogFragment, I'm doing something like this:

public class ProspectDialog extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

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

            View mView = inflater.inflate(R.layout.myLayout, null);
            builder.setView(mView);

            builder.setTitle(R.string.title);
            builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            AlertDialog dialog = builder.create();
            colorAlertDialogTitle(dialog);
            return dialog;
        }

    }

Upvotes: 0

Views: 559

Answers (2)

Mohammad
Mohammad

Reputation: 6148

Please look at the section "If you want to customize the AlertDialog a lot. For example adding some checkboxes with custom background color, use this approach:" of this post https://stackoverflow.com/a/33439849/5475941. In this post I showed how to change style of an AlertDialog dynamically. I hope it helps.

Upvotes: 1

Bö macht Blau
Bö macht Blau

Reputation: 13019

You can only acess UI elements (the divider line) after the AlertDialog is shown. Being created is not enough.

You can introduce a variable in your Fragment

private AlertDialog mDialog;

which you set in onCreateDialog()

mDialog = builder.create();

Then you can use your color changing method like this:

myDialogFragment.show(getSupportFragmentManager(), "coloredDialog");
myDialogFragment.colorAlertDialogTitle(myDialogFragment.getDialog());

(where getDialog() is the usual getter method)

Upvotes: 1

Related Questions