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);

If you are using a PreferenceActivity which you probably are, there is not one available.

You will need to do something like this:

    /**
 * Checks that a preference is a valid numerical value
 */
Preference.OnPreferenceChangeListener numberCheckListener = new OnPreferenceChangeListener() {

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        //Check that the string is an integer.
        return numberCheck(newValue);
    }
};

private boolean numberCheck(Object newValue) {
    if( !newValue.toString().equals("")  &&  newValue.toString().matches("\\d*") ) {
        return true;
    }
    else {
        Toast.makeText(ActivityUserPreferences.this, newValue+" "+getResources().getString(R.string.is_an_invalid_number), Toast.LENGTH_SHORT).show();
        return false;
    }
}


    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //get XML preferences
    addPreferencesFromResource(R.xml.user_preferences);


    //get a handle on preferences that require validation
    delayPreference = getPreferenceScreen().findPreference("pref_delay");

    //Validate numbers only
    delayPreference.setOnPreferenceChangeListener(numberCheckListener);

}

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" />

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