4

GOAL: Use EditTextPreference to take a number input from an user.

PROBLEM: I'm using support.v7.preference and, from what I've understood, this library bring problems to creates a custom DialogPreference (HowTo use support.v7.preference with AppCompat and potential drawbacks). Anyway I tried to create a NumberPickerPreference extending DialogPreference but I got ClassCastException.

Also I found another approach: programmatically set the input type of an EditTextPreference object like below:

    EditTextPreference pref = (EditTextPreference) findPreference("pref_edit_text");
    if (pref != null) {
       pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
    }

No way, I always get a ClassCastException .

Last approach I tried : modify the xml

android:inputType="numberDecimal"
android:digits="0123456789"

or android:numeric="integer"

No way again, on the Dialog I get the full keyboard, so the user can write everything.

Is there a way to properly enforce EditTextReference to take just number input?

Maicake
  • 1,046
  • 10
  • 34
  • it is reported of this being an issue, maybe this helps : https://github.com/Gericop/Android-Support-Preference-V7-Fix – mariuss Oct 10 '18 at 13:15

3 Answers3

1

Try one of these:

android:inputType="numberSigned" 
android:inputType="phone"

EDIT:

android:inputType="numberDecimal|numberSigned"
android:digits="0123456789"

EDIT:

There´s this lib that is supposed to make it work, if you´re willing to try:

https://github.com/Gericop/Android-Support-Preference-V7-Fix

Reading their github page you can find examples of custom inputs and their example code that looks similar to what you are doing now:

"The sample app shows an example of setting (via XML) and querying (programmatically) the input type of the EditTextPreference:

<EditTextPreference
    android:inputType="phone"
    android:key="edit_text_test" />

EditTextPreference etPref = (EditTextPreference) findPreference("edit_text_test");
if (etPref != null) {
    int inputType = etPref.getEditText().getInputType();
    // do something with inputType
}
Curious Mind
  • 659
  • 10
  • 26
1

You can do it by adding android: inputType = "numberPassword" to your XML file Then you can set your custom keyboard programmatically in your java like this:

 yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); 

The next thing you could do is to create a custom class:

  private class YourNumericKeyboardMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return source;
    }
}

The last step is to implement it to your EditText in order to make it display only the numbers:

 yourEditText.setTransformationMethod(new YourNumericKeyboardMethod());

After all this steps you should get a keyboard that has only numbers from 0 to 9 on it

byDavid360
  • 68
  • 9
  • I think that doens't work because it's more or less the same thing I was trying to do. You are using setInputType which is a method of EditText class, but I have an EditTextPreference object and I CAN'T cast it to Preference, to use GetEditText() in order to use setInputType. – Maicake Oct 11 '18 at 11:46
  • Oh, I see... You can access to the methods with something like this: – byDavid360 Oct 11 '18 at 11:53
  • EditTextPreference my_preference = (EditTextPreference) findPreference("pref_edit_text"); if (pref != null) { mypreference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER); } – byDavid360 Oct 11 '18 at 11:54
  • That's the problem. With support.v7 library you can't do that. It raises ClassCastException (fact that I already reported in the question ). – Maicake Oct 11 '18 at 12:03
0

Try to set inputType as the number.

android:inputType="number"
Jay Patel
  • 2,341
  • 2
  • 22
  • 43
Ramesh Yankati
  • 1,197
  • 9
  • 13