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()