Hide only numbers in EditText for pin-code (Android)

772 Views Asked by At

I have EditText in myApp with this TextWatcher and numberSigned input type:

TextWatcher() {
    @Override
    public void afterTextChanged(Editable s)
    {
        codeinput.removeTextChangedListener(this);
        String text = s.toString();
        text = text.replaceAll("[^0-9]", "");

        if (text.length() > 4)
           text = text.substring(0, 4);

        String newText = "";
        for (char c : text.toCharArray())
           newText += c + " ";
        text = newText.trim();

        String substring = "_ _ _ _".substring(text.length());
        text += substring;

        int length = text.replaceAll("([ ]?[_])+$", "").length();

        codeinput.setText(text);
        codeinput.setSelection(length);
        codeinput.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
};

When i change input type from numberSigned to numberPassword - all symbols in my EditText is hidden, i want to hide only numbers. How can i do it?

enter image description here

enter image description here

1

There are 1 best solutions below

4
On BEST ANSWER

I'm affraid that password type field hides all characters and you can't do anything with that

but consider using setLetterSpacing and don't use spaces (method available since API21)

if you need lower API support and you still want to use numberPassword then you have to set up 4 EditTexts - use editText.requestFocus for jumping to another/next View. or you may keep one EditText with numberSigned, but exchange during text entering all digits to asterisks (keeping digits in some separated value, as editText.getText will return you only asterisks)