I am changing the value of an EditText
on keyListener
.
But when I change the text the cursor is moving to the beginning of the EditText
. I need the cursor to be at the end of the text.
How to move the cursor to the end of the text in a EditText
.
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.
In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.
Simpler Solution to align text in EditText is to add android:gravity="center" in layout xml. There is no need to add Java code.
Try this:
UPDATE:
Kotlin:
editText.setSelection(editText.length())//placing cursor at the end of the text
Java:
editText.setSelection(editText.getText().length());
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