EditText
cursor position
Android development
set cursor
text input

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:

kotlin
val editText = findViewById<EditText>(R.id.messageInput)
editText.setText("Hello world")
editText.setSelection(5)

That places the cursor after the fifth character.

Move the Cursor to the End

This is the most common case:

kotlin
val text = editText.text
editText.setSelection(text.length)

This is useful after pre-filling a field, restoring draft text, or appending content programmatically.

In Java, the same pattern looks like this:

java
EditText editText = findViewById(R.id.messageInput);
editText.setText("Hello world");
editText.setSelection(editText.getText().length());

Select a Range Instead of One Position

setSelection(start, end) selects text between two positions:

kotlin
editText.setText("Hello world")
editText.setSelection(0, 5)

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:

  1. Set or update the text
  2. Call setSelection(...)

If the EditText is being updated during layout or after async work, post the selection change:

kotlin
1editText.setText("Loaded text")
2editText.post {
3    editText.setSelection(editText.text.length)
4}

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:

kotlin
1fun setSafeCursor(editText: EditText, index: Int) {
2    val safeIndex = index.coerceIn(0, editText.text.length)
3    editText.setSelection(safeIndex)
4}

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:

kotlin
1editText.requestFocus()
2editText.post {
3    editText.setSelection(editText.text.length)
4}

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 setSelection before setting the text.
  • Passing an index larger than the current text length.
  • Expecting selection to work before the EditText is 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.

Course illustration
Course illustration

All Rights Reserved.