How to show/hide buttons of custom dialog in preferences dynamically?

712 Views Asked by At

I have created a custom dialog that is used from a preference screen. Everything works fine except one thing: I want to switch the visibility of the Cancel button based on the status of an internal check.

Normally you have onPrepareDialog and onCreateDialog and you can do this in onCreateDialog. But here we have onPrepareDialogBuilder... so where is onCreateDialogBuilder? Where can I do something like

builder.setNegativeButton(null, null);

after onPrepareDialogBuilder? I cannot do it IN onPrepareDialogBuilder since I need the Cancel button in case the internal check fails.

Can you please help me to get into the right direction?

public UnlockPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setDialogLayoutResource(R.layout.dialog_enter_registration);
}

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);
    builder.setTitle(R.string.label_enter_registration);
}


// would need something like
@Override
protected void onCreateDialogBuilder(AlertDialog.Builder builder) {
    super.onCreateDialogBuilder(builder);
    if (internalCheckOk())
        builder.setNegativeButton(null, null);
    else
        builder.setNegativeButton(..., ...);
}
0

There are 0 best solutions below