Is there any way to set an
EditText in my XML preference screen to only accept number input?
In the xml file:
or during runtime:
editText.setRawInputType(TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL);
EditTextPreference supports all the same XML attributes as does an
EditText. So, use
Everyone is facing this issue because eclipse is not showing hint when we press the ctrl+space.
and paste. It will work like a charm
Fully agree with the answer. However I would like to add something here:Once you accept numeric input there is still a scenario where you may get error i.e when a user clicks submit on a blank EditText. Since you will in all probability be parsing your input to an int or double or float. A blank input will throw an error. I suggest you should check for error using try catch and NumberFormatException.
<EditTextPreference android:inputType="phone" android:key="phone_number" android:summary="@string/settings_phone_number_sum" android:title="@string/settings_phone_number" />
I enclose reading preference is try/catch NumberFormatException. If exception, I return default value.
I found it useful when migrating app from say float to int type preferences. Previous installations had float for preference and new one used int.
Of course one still needs to do validation on user entry.