In an Android app I let the user edit his preferences by running a class which extends PreferenceActivity.
In res\xml I have a file – preferences.xml – which has an xml-description of which fields the user can configure. Things like usernames and passwords. A sample snippet:
<EditTextPreference android:name="User Name"
android:summary="Name in Your Name/Company format"
android:defaultValue="" android:title="Login name" android:key="userName"
android:id="@+id/userName"/>
<EditTextPreference android:name="Password"
android:summary="Your web password" android:defaultValue=""
android:title="Login password" android:password="true" android:key="userPassword"
android:id="@+id/userPassword"/>
Things are working as expected. From my main app code I can access the values that the user has configured.
There is one thing that I hope to get some good advice on how to handle: Removing leading and trailing blank spaces in the preferences-values.
By user error or because of helpful keyboard apps, sometimes users enter an extra blank character in EditTextPreferences fields. Either as the first or the last character.
I would prefer if such obvious typing errors could be removed no later than when the user exits the PreferenceActivity.
I would like to get suggestions on a good way of cleaning up the user’s preferences.
Just use
String.trim()on the values provided by the user, before saving them to prefs. The trim() method removes all white-space characters from the front and end of the input string.http://developer.android.com/reference/java/lang/String.html#trim()