How to set cursor position in EditText?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android, you set the cursor position in an EditText with setSelection(...). The important part is not the method name but when you call it: the text must already be present, and the index must be inside the current text length.
Move the Cursor to a Specific Position
If you know the exact character index:
That places the cursor after the fifth character.
Move the Cursor to the End
This is the most common case:
This is useful after pre-filling a field, restoring draft text, or appending content programmatically.
In Java, the same pattern looks like this:
Select a Range Instead of One Position
setSelection(start, end) selects text between two positions:
That highlights "Hello" instead of placing a single caret.
Timing Matters
If you call setSelection before the text is actually assigned, the position can be wrong or throw an error. The safe order is:
- Set or update the text
- Call
setSelection(...)
If the EditText is being updated during layout or after async work, post the selection change:
Posting is often helpful when the field is attached to listeners or IME behavior that depends on the view being fully ready.
Guard Against Invalid Indexes
Selection positions must be between 0 and text.length. This is a safe helper:
This avoids IndexOutOfBoundsException when the desired position comes from external state.
Focus Can Matter Too
If the cursor does not appear where you expect, the issue may not be selection at all. The EditText may not have focus yet. In that case:
This is common when opening a screen and immediately trying to place the caret before the view hierarchy has fully settled.
Setting Selection After TextWatcher Changes
Some inputs are modified by a TextWatcher, for example when inserting formatting characters into phone numbers or currency fields. If the watcher changes the text, it can also move the selection unexpectedly. In those cases, update the selection after the text transformation finishes and clamp it to the new text length.
That is why cursor bugs in formatted fields often look inconsistent. The code sets one position, then the watcher changes the text and the cursor ends up elsewhere.
Selection and User Experience
Cursor control is not just a technical detail. Good selection behavior makes forms feel stable and predictable, especially when restoring state or helping the user edit prefilled text in the middle instead of always forcing the caret to the end. That small detail is noticeable to users.
Common Pitfalls
- Calling
setSelectionbefore setting the text. - Passing an index larger than the current text length.
- Expecting selection to work before the
EditTextis ready in the UI lifecycle. - Forgetting that
setSelection(start, end)creates a text selection, not just a cursor move.
Summary
- Use
setSelection(position)to move the cursor. - Use
setSelection(start, end)to select a text range. - Set the text before setting the selection.
- Clamp the index if it may be out of bounds.
- If timing is tricky, post the selection change to the view.

