SetInputType in EditTextPreference

how can I add propeta setInputType to EditTextPreference (my goal is to set the input type to numbers only), I tried:

editTextPref.setInputType(InputType.TYPE_CLASS_NUMBER); 

but this seems to work only for EditTexts, not EditTextPreferences

+7
source share
3 answers

You can get the EditText from the Preference and from there setInputTypes or use KeyListeners to tell the keyboard:

 EditText et = (EditText) editTextPref.getEditText(); et.setKeyListener(DigitsKeyListener.getInstance()); 
+8
source

If you really need one input type for your editTextPreference, you can set it in XML using android: inputType = "number" or android: inputType = "numberDecimal".

Example:

 <EditTextPreference android:defaultValue="130" android:dialogMessage="@string/upper_limit_hint" android:dialogTitle="@string/upper_limit_text" android:inputType="number" android:key="UPPER_LIMIT" android:maxLength="4" android:summary="@string/upper_limit_hint" android:title="@string/upper_limit_text" /> 

Also, higher than Dave's suggestion above, I was able to achieve this programmatically by assigning EditTextPreference to the TextView (I could not use EditText).

Example:

 EditTextPreference editTextPreference = (EditTextPreference) preference; TextView etpTextView = (TextView) editTextPreference.getEditText(); // If I want entry with decimal capability etpTextView.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); // or if I want entry without decimal capability etpTextView.setInputType(InputType.TYPE_CLASS_NUMBER); 
+4
source

try it

  • call getEditText () from your editTextPref
  • then set the input type for it.
0
source

All Articles