I have an EditTextPreference
in the PreferenceActivity
. When user click the EditTextPreference
will show a dialog. In the dialog, user can input a value, and the dialog has "OK" and "Cancel" buttons. I want to call the click event of ok button to check the value, but I do not know how to call the click even.
I know I can use EditTextPreference.setOnPreferenceChangeListener()
, but I want to know if I can use OK button click event.
Actually you can't since the preference is using an internal
AlertDialog.Builder
and creates a new dialog every time you click the preference. The next problem is that the dialog builder sets the click listener for you and if you override them you might destroy the close behavior of the button click.This bothered me since I wanted a preference which only closes on valid input (otherwise a toast is shown and user should press cancel if he can't get it right).
(If you really need a solution for exactly this problem) You can find general solution of a validating
DialogPreference
here and a validatingEditTextPreference
here which I wrote myself.You can extend EditTextPreference to get control over the click handler.
In the Xml instead of
<EditTextPreference/>
reference it like this:Your preference activity doesn't appear to be implementing a
You may want to read over the excellent answer to the question: Updating EditPreference