Question

How to Set Text in an EditText in Kotlin

kotlinandroidandroid-edittext

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 String is like a printed sentence on a separate piece of paper.
  • An Editable is 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 value
  • Editable = text object the input field can edit
  • setText(...) = the safe way to put text into the field
Take Quiz

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 the EditText view.
  • setText("Delhi") displays the string inside the field.
  • text returns an Editable.
  • toString() converts that editable content into a regular Kotlin String.

If you really want to assign to text

You would need an Editable, not a String:

import android.text.Editable

nameTxt.text = Editable.Factory.getInstance().newEditable(name)

This works, but it is less common than using setText(name) for simple cases.

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:

  1. val name = "Paramjeet"

    • A Kotlin String variable is created.
  2. val nameTxt = findViewById<EditText>(R.id.nametxt)

    • Android finds the EditText from your layout.
  3. nameTxt.setText(name)

    • The String value is passed to the view.
    • Android updates the EditText's internal editable content.
  4. val result = nameTxt.text.toString()

    • nameTxt.text returns an Editable.
    • .toString() converts it into a plain String.
  5. result now contains:

"Paramjeet"

If you had written this instead:

nameTxt.text = name

Android would reject it because name is a String, but text expects an Editable.

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)
Take Quiz

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:

  • Use setText(...) to display text
  • Use text.toString() to read text
  • Validate the string before saving or sending it to an API

Common Mistakes

1. Assigning a String directly to text

Broken code:

val name = "Paramjeet"
nameTxt.text = name

Why it fails:

  • text expects Editable
  • name is a String

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

Many beginners think Kotlin should avoid Java-style methods entirely.

Reality:

  • Android APIs were largely designed in Java.
  • Kotlin can call Java methods directly.
  • Using setText() in Kotlin is completely correct.

4. Mixing old-style casts with cleaner Kotlin syntax

Less idiomatic:

val nameTxt = findViewById(R.id.nametxt) as EditText

Better:

val nameTxt = findViewById<EditText>(R.id.nametxt)

5. Forgetting null handling in some view setups

In some contexts, your view may not exist yet when you try to access it.

Avoid accessing views before setContentView(...) in an Activity or before the view is created in a Fragment.

Comparisons

TaskCorrect approachResult type
Put text into EditTexteditText.setText("Hello")Updates UI text
Read text from EditTexteditText.text.toString()String
Access raw editable contenteditText.textEditable
Assign directly to texteditText.text = editableValueRequires Editable

String vs Editable

TypeWhat it isMutable?Common use
StringRegular Kotlin textNoApp logic, API values, variables
EditableAndroid editable text objectYesInternal text used by EditText

text property vs setText()

OptionBest forNotes
editText.textReading or working with editable contentReturns Editable
editText.setText(...)Setting visible text from a stringMost common for input fields
Take Quiz

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.text returns Editable
  • A plain Kotlin string is String
  • Use setText(stringValue) to display text in EditText
  • Use text.toString() to get a String from 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 Editable?

Yes:

Editable.Factory.getInstance().newEditable("Hello")

But for normal UI code, setText() is simpler.

Why does Kotlin allow Java-style methods like setText()?

Because Kotlin is fully interoperable with Java, especially on Android where many framework APIs come from Java.

Take Quiz

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

  1. Create an EditText for the user's name.
  2. Set a default name into the EditText when the screen loads.
  3. Add a button that reads the current text from the EditText.
  4. Convert the input to a String before using it.
  5. Display the result in a TextView.
Take Quiz

Keep learning

Related questions

View more