Edit: The previous answers below were built in stock android.preference.EditTextPreference and unfortunately do not work for android.support.v7.preference.EditTextPreference .
In android.preference.EditTextPreference , the EditText control is created programmatically and the AttributeSet from the Preference is passed to it.
android.preference.EditTextPreference Source:
public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); mEditText = new EditText(context, attrs);
White allows us to set inputType on the Preference itself and pass it to EditText . Unfortunately android.support.v7.preference.EditTextPreference creates an EditText in Layout
See this question for ideas on working on this:
I just want to tell you that the subclass is EditTextPreferenceDialogFragment and override onAddEditTextToDialogView, as well as overriding PreferenceFragmentCompat # onDisplayPreferenceDialog to show that the subclass works fine as needed, thanks for the help.
Create your own class that extends EditTextPreference and sets it there.
Here is my EditIntegerPreference class:
public class EditIntegerPreference extends EditTextPreference { public EditIntegerPreference(Context context) { super(context); } public EditIntegerPreference(Context context, AttributeSet attributeSet) { super(context, attributeSet); } public EditIntegerPreference(Context context, AttributeSet attributeSet, int defStyle) { super(context, attributeSet, defStyle); getEditText().setInputType(InputType.TYPE_CLASS_NUMBER); getEditText().setSelectAllOnFocus(true); } @Override public String getText() { try { return String.valueOf(getSharedPreferences().getInt(getKey(), 0)); } catch (Exception e) { return getSharedPreferences().getString(getKey(), "0"); } } @Override public void setText(String text) { try { if (getSharedPreferences() != null) { getSharedPreferences().edit().putInt(getKey(), Integer.parseInt(text)).commit(); } } catch (Exception e) {
Note that you can add the inputType attribute to EditTextPreference
android:inputType="number"
The reason I didn’t choose this route is because I wanted my preference to be saved as Integer , not String