Search code examples
androidkeyboard

SetInputType(InputType.TYPE_CLASS_NUMBER) and setInputType( InputType.InputType.TYPE_NUMBER_VARIATION_PASSWORD) give out different keyboards


I'm trying to make two fields with a password and numbers on the same screen. But for them, a different keyboard is called by default, which does not look pretty. How can this be fixed?

setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD) 

and

setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL) 

give out different keyboards. How can I fix it?

1. First input field:

otpField.getEditText().setType(TYPE_FORMATTED_INVISIBLE_MASK);
otpField.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);

enter image description here

2. Second input field:

ePINField.getEditText().setType(TYPE_FORMATTED_INVISIBLE_MASK);
ePINField.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

enter image description here


Solution

  • A very simple way, but not advised, would be for you to define the two fields as password and make the first EditText visible. This doesn't make sense, since the first one is not a password, but it's a way to do what you want (I don't advise, as this would get in the way of the accessibility).

    otpField.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
    otpField.getEditText().setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            
    ePINField.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
    

    The setTransformationMethod() method will leave the first EditText as if it were a "normal" number field, after all it will have the digits visible.

    In this way, the two keyboards will be visually identical.