Number Preferences in Preference Activity in Android Number Preferences in Preference Activity in Android android android

Number Preferences in Preference Activity in Android


Use an EditTextPreference and set the input type to TYPE_CLASS_NUMBER. This will force the user to enter numbers and not letters.

EditTextPreference pref = (EditTextPreference)findPreference("preference_name");pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);


You can also enforce it with the xml attribute android:numeric. The possible relevant values for this attribute are decimal and integer.


You can also do this directly in your preferences.xml. Something like this would work:

<EditTextPreference    android:defaultValue="100"    android:dialogTitle="@string/pref_query_limit"    android:inputType="number"    android:key="pref_query_limit"    android:summary="@string/pref_query_limit_summ"    android:title="@string/pref_query_limit" />