I have an EditPreference in a PreferenceActivity and I have a variable that tells me if I should allow the user to access this preference or to show some alert.
My problem is that I couldn’t find how to cancel the preference dialog before it’s displayed and to show my alert (according to the variable).
I tried to return true/false in the preference onClick or in onTreeClick but that didn’t do anything, the dialog still popped.
On Android 2.1+ .
Thanks.
The
DialogPreference.onClick(), which handles clicks to the preference itself, isprotected, so you can’t override it in your ownPreferenceActivityclass members.However, you can extend the class to achieve what you need. Below is a very minimalist example:
In your XML file:
In your
PreferenceActivity:As a final note, let me say that whenever you can’t find a way to do what you want, think about taking a look at how the Android classes themselves work. Most of the times, they will give you good insights to achieve what you want.
In this case, it’s the
DialogInterface.onClick()method, as described above. So you know you need to override it somehow to achieve that. In this case, the solution is extending theEditTextPreferenceclass itself.