Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move cursor to end of EditText?

Tags:

I'm replacing some text in an EditText, then I want to move the cursor to the end of the current text input. It kind of seems to work - when I start typing again, the cursor seems to be at the end of the EditText, but it doesn't flash / blink anymore. I have to touch the EditText again for the cursor to start blinking again. Am I doing it wrong?:

editText.setSelection(editText.getText().length()-1); 

Thanks

like image 967
user291701 Avatar asked Oct 28 '11 21:10

user291701


People also ask

How do I move my cursor to 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.


1 Answers

One trick I used was setting the text to "" and then appending the entire string I wanted.

i.e.

String newtext = editText.getText().toString() + "the new text"; editText.setText(""); editText.append(newtext); 

This seems to place the cursor in the right place for me.

like image 161
tarrant Avatar answered Oct 04 '22 17:10

tarrant