Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceActivity validating user input and showing error messages

I am using a PreferenceActivity for user to define certain settings for the application.

I am also using the OnPreferenceChangeListener interface to get notifications of changed preferences, and within that callback, I can do validation of the user's input, and I can make decisions on updating on rejecting user's input.

When I detect a validation error, I would like to keep the preference editing dialog box active on the screen, so that I can use the .getTextEdit().setError() method to set an error message to inform the user. However, I wasn't able to manage to keep the dialog box active on the screen, since it seems like disappearing right before the OnPreferenceChangeListener callback gets executed.

Any ideas?

like image 263
Cinar Avatar asked Jan 21 '11 02:01

Cinar


1 Answers

Well, there isn't an event for you to use BEFORE the user has finished has edit, as written in android dev:

The interface has only one callback method, onSharedPreferenceChanged()

What I would do is implement a custom preference, (as explained in http://developer.android.com/guide/topics/ui/settings.html#Custom) But instead of extending DialogPreference i would extend EditTextPreference. Haven't tested it though, but sounds like it should work. Good luck :)

like image 198
Sean Avatar answered Nov 12 '22 06:11

Sean