Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInputType on EditTextPreference

how can i add the "setInputType" propety to an EditTextPreference (my goal is to set the input type to numbers only), i've tried:

editTextPref.setInputType(InputType.TYPE_CLASS_NUMBER);

but this only seems to work for EditTexts, not EditTextPreferences

like image 388
92Jacko Avatar asked Sep 06 '11 12:09

92Jacko


2 Answers

You can retrieve the EditText from the Preference and from there setInputTypes or use KeyListeners to inform the keyboard:

EditText et = (EditText) editTextPref.getEditText();
et.setKeyListener(DigitsKeyListener.getInstance());
like image 139
David Snabel-Caunt Avatar answered Nov 19 '22 23:11

David Snabel-Caunt


If all you really need is a single inputType for your editTextPreference you could set it in the XML with 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, thanx to Dave's suggestion above, I was able to achieve this programmatically by assigning the EditTextPreference to a TextView (I was unable to use an 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);
like image 4
William Bell Avatar answered Nov 19 '22 22:11

William Bell