Setting text in EditText Kotlin

Use setText(String), since editText.text expects an Editable, not a String.


Use setText(String) as EditText.text requires an editable at firstplace not String

WHY ?

Nice explanation by Michael given under this link. Do visit this link for more detail

When generating a synthetic property for a Java getter/setter pair Kotlin first looks for a getter. The getter is enough to create a synthetic property with a type of the getter. On the other hand the property will not be created if only a setter presents.

When a setter comes into play property creation becomes more difficult. The reason is that the getter and the setter may have different type. Moreover, the getter and/or the setter may be overridden in a subclass.


There are several working answers here, but if you still want to use the property format and have your code look clean, you could write an extension:

fun String.toEditable(): Editable =  Editable.Factory.getInstance().newEditable(this)

You can then use it as such:

mEditText.text = myString.toEditable()

If you want to use getter .text from principle, use:

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

Simple Solution

Just use edittext.setText(yourdata) instead of edittext.text because the EditText is editable, the edittext.text is used for TextView

For Example:

var name:String = "Muzammil"
edittext.setText(name)

That's it its work for me.