Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to set the EditText Cursor to the end of its text [duplicate]

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?

like image 626
William Kinaan Avatar asked Feb 03 '13 14:02

William Kinaan


People also ask

How do I keep my cursor at the end of EditText?

setSelection(editText. getText(). length()); This places cursor to end of EditText.

How do I change the cursor position in 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.

What is the use of android inputType attribute for EditText in any layout XML file *?

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.

What is the function of setting autoText attribute in EditText?

android:autoText If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.


1 Answers

You have two options, both should work:

a)

editText.setText("Your text"); editText.setSelection(editText.getText().length()); 

b)

editText.setText("");     editText.append("Your text"); 
like image 75
jenzz Avatar answered Oct 15 '22 19:10

jenzz