Get positive Button in DialogPreference
Asked Answered
D

1

14

Is there a way to get the positive button of a DialogPreference in any of its events?

I would like to disable it initially, and make it enabled once a EditText validates.

Dilatometer answered 7/6, 2012 at 12:17 Comment(0)
H
35

Simply call

 (AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

If you are using a Dialog Builder though, make sure you call this after builder.create() and before .show()

You can then keep a reference to the dialog and enable the button once your conditions are fulfilled.

Heiskell answered 7/6, 2012 at 14:29 Comment(5)
Yes, this is correct. getDialog() is working in showDialog, which I override. Thanks for your help.Dilatometer
AlertDialog dialog = (AlertDialog) getDialog(); Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE); ok.setEnabled(false);Lyon
It worked only after BOTH builder.create() and show() method executed. Before show() return was null.Szymanski
@Szymanski is correct, the button if null is you have show() after. Guykun's code is otherwise correct.Bomke
Is there any way to override showDialog with android.support.v7.preference.DialogPreference?Currish

© 2022 - 2024 — McMap. All rights reserved.