edittext.settext() changes the keyboard type to default [ from ?123 to ABC]
Asked Answered
O

3

17

I have following code for my edittext formatting, since it can take any input I am not setting any input type:

if (cardNumberEditText != null) {
    cardNumberEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int currSel = cardNumberEditText.getSelectionStart();
            cardNumberEditText.removeTextChangedListener(textWatcher);
            .
            .
            cardNumberEditText.setText(formattedNumber);
            .
            .
            cardNumberEditText.setSelection(currSel);
            cardNumberEditText.addTextChangedListener(textWatcher);
        }

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

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

So initially I get the default input type which is ABC, now when I change it to ?123 (using ABC/123? toggel button) and after entering some number the keyboard changes back to ABC. This code seams to work fine on samsung devices s3 and sywpe but not on nexus with L and HTC one

When I comment all the code inside onTextChanged, it works fine. So when I investigated I found out that culprit is cardNumberEditText.setText(formattedNumber);

I am not setting any input type, I am just using the ABC/?123 toggle key on keyboard for switching

Any help/suggestion why this is happening (on few devices) and how can I correct it ??

Optical answered 14/10, 2014 at 16:34 Comment(5)
Aren't you telling it to bring up the numeric keyboard by inputting a number? Or am I not understanding?Panter
no i am not telling it by writing any code for input type, I am using the keyboad key ABC/123 to switch between themOptical
finally found the answer here, will try it outOptical
unfortunately the above one doesn't work well with me, any other better solution ???Optical
found one more solution, using append instead of settext, but still doesn't work with my case, as I am doing card formatting I need to settext at the endOptical
O
41

finnaly got it working, had to combine multiple solutions mentioned in the comments above

since the guilty was settext, I found a replacement for it - append

but to use append I had to clear edittext without using settext, this link to the rescue

so replaced

cardNumberEditText.setText(formattedNumber);

with

cardNumberEditText.getText().clear();
cardNumberEditText.append(formattedNumber);

works like a charm now

Optical answered 15/10, 2014 at 3:9 Comment(6)
append helped me in my scenerio.->edittext which has some a input enetered.settext will remove/clears the entire input.replacing with new settext value. use append if you dont want input to be removed.Sandlin
Unfortunately this causes Galaxy S III 4.3 not to clear previous text properly and buggy device appends the text twice, but I get good results with s.replace(0, s.length(), formatted, 0, formatted.length()); in afterTextChanged(Editable s) .Tiebold
Append method is fixing the keyboard switching behaivor. But there is a minor problem. My edittext is numeric because I want to prevent user entering anything but numbers. When I used settext I could set a text that contains space character. When I use append it doesn't allow me to append anything but numbers 8(Cough
You saved my dayStewardess
@EvrenOzturk this will be fixed by adding android:digits="0123456789 " to the xml, apparently whatever you append gets filtered with android:inputType but the filter will be "OR"ed with android:digitsQuintain
You saved our day!Athiste
M
0

I had a similar problem, but keyboard was changed to default after I call scrollView.fullScroll(NestedScrollView.FOCUS_DOWN) when keyboard is shown I just replaced fullScroll method to smoothScrollTo and all work fine

Mita answered 7/4, 2021 at 11:26 Comment(0)
B
0

In addition to the accepted answer, if anyone else struggles with cursor moving to the end after .append(), here is how you can retain cursor position:

val cursor = editText.selectionEnd
editText.text?.clear()
editText.append(input)
editText.setSelectionSafe(cursor)

where setSelectionSafe() is an extension I use to prevent crushes

fun EditText?.setSelectionSafe(index: Int) {
    if (this == null || index > this.length()) return
    this.setSelection(index)
}

this works both for entering and deleting !

Barta answered 10/9, 2023 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.