Why is the cursor in EditText a pain for beginners?
\nImagine: you're writing a notes or chat app. The user opens an existing entry, starts typing — and suddenly the new text is inserted at the beginning or in the middle of the line. Sound familiar? This is one of those small but incredibly annoying problems that everyone starting to learn Android development in Kotlin encounters.
\nOn Stack Overflow, the question "Place cursor at the end of text in EditText" has gathered thousands of views and dozens of answers. Why? Because there is no intuitive way to "just place the cursor at the end" in the Android SDK. You need to know a few tricks. Today, we'll break them down once and for all — simply, clearly, and with code examples.
\nBy the way, if you're coding not only for Android but also, for example, a backend in Python — on our website PythonLib you'll find plenty of materials to get started. But today, we're talking about mobile development.
\n\nThe classic solution: setSelection()
\nThe main tool for controlling the cursor in EditText is the setSelection() method. It takes the index of the position where you want to move the cursor. But to place the cursor at the very end, we first need to know the length of the text.
// Simple way: get the text length and pass it to setSelection\neditText.setText("Hello, world!")\neditText.setSelection(editText.text.length)\n\nThat's it! editText.text.length returns the number of characters. Pass this number to setSelection() — and the cursor ends up after the last character.
But there's a catch: when to call it?
\nBeginners often write this code right after setText() — and are surprised that nothing works. The problem is that the EditText may not have updated yet. This is especially relevant if you are setting the text programmatically in a fragment or activity.
The best solution is to use post() or an onTextChanged listener. Here's an example with post() — it guarantees the code will execute after the View is ready:
editText.setText("Long text that needs editing")\neditText.post {\n editText.setSelection(editText.text.length)\n}\n\nNow the cursor will definitely end up at the end. This method is your savior in 90% of cases.
\n\nWhat if the text changes dynamically?
\nLet's say you're writing a search field that auto-suggests hints. Or an editor where the user can insert templates. In such situations, you need to place the cursor at the end every time the text changes programmatically.
\nUse a TextWatcher or, more simply, just call setSelection() after each change:
fun appendText(newText: String) {\n val currentText = editText.text.toString()\n editText.setText(currentText + newText)\n editText.setSelection(editText.text.length)\n}\n\nThis code first appends the new text to the existing one, then moves the cursor to the end. It works flawlessly.
\n\nA trick for experienced developers: Editable and Spannable
\nIf you're working directly with Editable (for example, getting it from editText.text), you can use SpannableStringBuilder and the setSpan() method. But for the basic task of "cursor at the end," this is overkill. Remember: post { setSelection(length) } is your go-to technique.
Why doesn't this work in dialogs and fragments?
\nAnother frequent question on Stack Overflow. If your EditText is inside an AlertDialog or BottomSheetDialogFragment, the code might not work because the View is not yet attached to the window.
The solution is to use onResume() or onWindowFocusChanged(). In