The user has to enter his mobile number, the mobile number has to be 10 numbers, I used TextWatcher
to do that, like this
et_mobile.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub if (et_mobile.getText().toString().length() > 10) { et_mobile.setText(et_mobile.getText().toString() .subSequence(0, 10)); tv_mobileError.setText("Just 10 Number"); }else{ tv_mobileError.setText("*"); } } });
but the problem is when the user enters the 11th number, the cursor of the edittext starts from the beginning of the text, I want it to still at the end, how?
setSelection(editText. getText(). length()); This places cursor to end of EditText.
Say in your xml's edittext section, add android:paddingLeft="100dp" This will move your start position of cursor 100dp right from left end. Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.
The android:inputType attribute allows you to specify various behaviors for the input method. Most importantly, if your text field is intended for basic text input (such as for a text message), you should enable auto spelling correction with the "textAutoCorrect" value.
android:autoText If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.
You have two options, both should work:
a)
editText.setText("Your text"); editText.setSelection(editText.getText().length());
b)
editText.setText(""); editText.append("Your text");
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