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 ??
This example demonstrates how do I set only numeric value for editText in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box. this should be the accepted answer, +1 for simplicity.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With