Question
I am trying to set text in an EditText in Kotlin, but I get this error:
Type mismatch.
Required: Editable
Found: String
My code looks like this:
val name = "Paramjeet"
val nameTxt = findViewById<EditText>(R.id.nametxt)
nameTxt.text = name
Why does this happen, and what is the correct way to assign a String value to an EditText in Kotlin without confusing Java-style syntax with Kotlin usage?
Short Answer
By the end of this page, you will understand why EditText.text and String are different types in Android Kotlin, how to correctly put text into an EditText, when to use setText(), and how to read user input safely using text.toString().
Concept
In Android, an EditText is not just a plain string holder. Its text property is typically an Editable, which is a mutable text object used by the Android UI system.
A String in Kotlin is an ordinary immutable text value like:
val name = "Paramjeet"
But EditText.text is designed to represent editable content inside the input field. That is why this causes a type mismatch:
nameTxt.text = name
You are trying to assign a String where Android expects an Editable.
The usual and correct way to place a string into an EditText is:
nameTxt.setText(name)
Even in Kotlin, calling Java methods is completely normal. Kotlin runs on the JVM and works with Android's Java-based APIs all the time.
This matters in real programming because Android views often expose properties and methods with different types for reading and writing. Understanding the difference helps you avoid type errors and write code that matches the Android framework correctly.
Mental Model
Think of an EditText like a notebook page.
- A
Stringis like a printed sentence on a separate piece of paper. - An
Editableis like the actual writable page inside the notebook.
You cannot replace the notebook's writable page by directly handing it a plain printed sentence. Instead, you ask the notebook to write that sentence onto the page:
nameTxt.setText(name)
So:
String= plain text valueEditable= text object the input field can editsetText(...)= the safe way to put text into the field
Syntax and Examples
Core syntax
Set text in an EditText
val name = "Paramjeet"
val nameTxt = findViewById<EditText>(R.id.nametxt)
nameTxt.setText(name)
Read text from an EditText
val enteredName = nameTxt.text.toString()
Example
val cityInput = findViewById<EditText>(R.id.cityInput)
cityInput.setText("Delhi")
val city = cityInput.text.toString()
println(city)
Explanation
findViewById<EditText>(...)gets theEditTextview.setText("Delhi")displays the string inside the field.textreturns anEditable.toString()converts that editable content into a regular KotlinString.
If you really want to assign to
Step by Step Execution
Consider this code:
val name = "Paramjeet"
val nameTxt = findViewById<EditText>(R.id.nametxt)
nameTxt.setText(name)
val result = nameTxt.text.toString()
Here is what happens step by step:
-
val name = "Paramjeet"- A Kotlin
Stringvariable is created.
- A Kotlin
-
val nameTxt = findViewById<EditText>(R.id.nametxt)- Android finds the
EditTextfrom your layout.
- Android finds the
-
nameTxt.setText(name)- The
Stringvalue is passed to the view. - Android updates the
EditText's internal editable content.
- The
-
val result = nameTxt.text.toString()nameTxt.textreturns anEditable..toString()converts it into a plain .
Real World Use Cases
Setting text in an EditText is common in many Android apps.
Common examples
-
Prefilling profile forms
- Show a saved name, email, or phone number when a screen opens.
-
Editing existing data
- Load a note, address, or task title into an input field for editing.
-
Restoring saved state
- Put previously entered text back after configuration changes or app restarts.
-
Showing API data in input fields
- Fetch user details from a server and display them in editable form fields.
-
Search screens
- Restore the previous search query into a search input.
Example
val emailInput = findViewById<EditText>(R.id.emailInput)
val savedEmail = "user@example.com"
emailInput.setText(savedEmail)
Real Codebase Usage
In real Android codebases, developers often use EditText together with validation, formatting, and early checks.
Common patterns
Prefill and validate
nameInput.setText(user.name)
val enteredName = nameInput.text.toString().trim()
if (enteredName.isEmpty()) {
nameInput.error = "Name is required"
return
}
Guard clause before saving
val title = titleInput.text.toString().trim()
if (title.isBlank()) return
Load model data into form fields
fun bindUser(user: User) {
nameInput.setText(user.name)
emailInput.setText(user.email)
}
Error handling with parsing
val ageText = ageInput.text.toString()
val age = ageText.toIntOrNull()
if (age == null) {
ageInput.error = "Enter a valid number"
}
Key takeaway
In production code:
Common Mistakes
1. Assigning a String directly to text
Broken code:
val name = "Paramjeet"
nameTxt.text = name
Why it fails:
textexpectsEditablenameis aString
Fix:
nameTxt.setText(name)
2. Forgetting toString() when reading input
Broken code:
val name = nameTxt.text
This gives you an Editable, not a normal String.
Fix:
val name = nameTxt.text.toString()
3. Assuming setText() is Java-only
Comparisons
| Task | Correct approach | Result type |
|---|---|---|
Put text into EditText | editText.setText("Hello") | Updates UI text |
Read text from EditText | editText.text.toString() | String |
| Access raw editable content | editText.text | Editable |
Assign directly to text | editText.text = editableValue | Requires Editable |
String vs
Cheat Sheet
// Find the EditText
val nameTxt = findViewById<EditText>(R.id.nametxt)
// Set text
nameTxt.setText("Paramjeet")
// Read text as String
val name = nameTxt.text.toString()
Rules to remember
EditText.textreturnsEditable- A plain Kotlin string is
String - Use
setText(stringValue)to display text inEditText - Use
text.toString()to get aStringfrom user input
If you must assign to text
nameTxt.text = Editable.Factory.getInstance().newEditable("Paramjeet")
Common error
nameTxt.text = "Paramjeet" // Type mismatch
FAQ
Why does EditText.text expect Editable instead of String?
Because Android stores editable input as an Editable object so the text can be modified efficiently inside the UI component.
Can I use setText() in Kotlin?
Yes. Kotlin works with Java-based Android APIs, and setText() is the standard way to set text in an EditText.
How do I get a string from an EditText in Kotlin?
Use:
val value = editText.text.toString()
Is editText.text the same as editText.text.toString()?
No. editText.text is an Editable. editText.text.toString() is a regular Kotlin String.
Can I convert a String to ?
Mini Project
Description
Build a simple profile form screen with one EditText for a user's name and one button to show the entered value. This demonstrates both sides of the concept: setting initial text into an EditText and reading it back as a String.
Goal
Prefill an EditText with a default name, let the user edit it, and display the final text when a button is pressed.
Requirements
- Create an
EditTextfor the user's name. - Set a default name into the
EditTextwhen the screen loads. - Add a button that reads the current text from the
EditText. - Convert the input to a
Stringbefore using it. - Display the result in a
TextView.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.
Android Foreground Service Notification Channels in Kotlin
Learn why startForeground fails on Android 8.1 and how to create a valid notification channel for foreground services in Kotlin.