Reputation: 523
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
Upvotes: 7
Views: 5610
Reputation: 39
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);
Upvotes: 4
Reputation: 6602
Try This
Upvotes: 0
Reputation: 58371
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());
Upvotes: 8