268

I am trying to set text in a EditText but it says:

Type mismatch. 
Required: Editable 
Found: String

My code is as follow:

String name = "Paramjeet"
val nametxt = findViewById (R.id.nametxt) as EditText
nametxt.text = name

Don't say to use setText because I am using kotlin, not Java.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Paramjeet Singh
  • 2,930
  • 2
  • 9
  • 17
  • Possible duplicate of [How does Kotlin property access syntax work for Java classes?](https://stackoverflow.com/questions/37374075/how-does-kotlin-property-access-syntax-work-for-java-classes) – Vladyslav Matviienko Jun 12 '17 at 07:46
  • Aside: You don't need findViewById if you use the Kotlin Android extensions (shipped with Android Studio). You should just be able to call `nexttxt.setText(name)` without any find or casting. – charles-allen Jan 03 '18 at 13:12
  • 3
    This is not a Kotlin code as you are claiming @Singh – Sazzad Hissain Khan Jul 09 '19 at 08:05
  • @VladyslavMatviienko: Yeah, we should remove this thread because everyone who can't set text in their kotlin EditText would immediately think to search for "kotlin property access syntax for java classes." It's the first thing that popped up in my head when I saw that compiler error. – SMBiggs Aug 10 '20 at 14:48

15 Answers15

521

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

nhaarman
  • 98,571
  • 55
  • 246
  • 278
51

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.

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
44

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()
Ben
  • 706
  • 7
  • 11
25

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

nametxt.text = Editable.Factory.getInstance().newEditable(name)
Evgeniy Pavlov
  • 429
  • 8
  • 12
25

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.

Simson
  • 3,373
  • 2
  • 24
  • 38
Muzammil
  • 688
  • 7
  • 7
9

Or cast to TextView but I believe this should be fixed on kotlin side for sure for convenience of developers !

(someEditText as TextView).text = "someTextValue"

Or with some extensions:

val EditText.asTextView: TextView get() = this as TextView

var EditText.value: CharSequence? 
    get() = asTextView.text
    set(value) {
        asTextView.text = value
    }

You can write:

someEditText.asTextView.text = "someTextValue"

or

someEditText.value = "someTextValue"

But sadly you just cannot write simple someEditText.text = "someTextValue"

Renetik
  • 5,887
  • 1
  • 47
  • 66
8

Or you can use an extension property:

var EditText.value
    get() = this.text.toString()
    set(value) {
            this.setText(value)
    }

and use .value= instead of .text=

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
letroll
  • 1,059
  • 1
  • 14
  • 36
3

Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin. But, While generating a property for a Java getter/setter pair Kotlin at first looks for a getter. The getter is enough to infer the type of property from the type of the getter. On the other hand, the property will not be created if only a setter is present( because Kotlin does not support set-only properties at this time ) .

When a setter comes into play, property generation process becomes a bit ambiguous. 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 , which exactly is the case of EditText in android.

In above case the Android TextView class contains a getter

CharSequence getText() 

and a setter void

setText(CharSequence)

If I had a variable of type TextView my code would have worked fine. But I used EditText class which contains an overridden getter

Editable getText()

which means that you can get an Editable for an EditText and set an Editable to an EditText. Therefore, Kotlin reasonably creates a synthetic property text of type Editable. As String class is not Editable, that’s why I cannot assign a String instance to the text property of the EditText class.

It Seems like JetBrains forgot to specify the dominant role of getter methods while generating kotlin properties for Java getter and setter methods. Anyways, I have submitted pull request to Jet brains kotlin website through github.

I have detailed above problem in this medium post too How Does Kotlin Generate Property from Java Getters and Setters (Undocumented by Jetbrains)

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34
2

Use like this:

edtTitle.setText(intent.getStringExtra(EXTRA_TITLE))
edtDesc.setText(intent.getStringExtra(EXTRA_DESC))
adiga
  • 34,372
  • 9
  • 61
  • 83
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
1

I had the same issue in my projects, I give you an example that shows how to retrieve and set data in the layouts using Kotlin: there is one button save_button and two text edit field edit_name and edit_password.

 //when cliquing on the button 'save_button' 
    save_button.setOnClickListener {
    // geting the value from the two fields by using .text.toString()
                val email =  edit_name.text.toString()
                val password = edit_password.text.toString()
    // showing the result on the systeme's log 
                    Log.d("Main activity","your email is " + email )
                    Log.d("Main activity", "your password is $password" )
    // Then shows these values into the text view palete using  .setText()
                    text_view.setText("$email " + "$password")
                }
Sadiki Ayoub
  • 101
  • 1
  • 10
1
val editable = Editable.Factory.getInstance().newEditable(savedString)

editText.text = editable
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
0

setText(String), so you must setText your string to editText, so in your case is : nametxt.setText(name)

-2
var name:String = "hello world"

textview.text = name

(or)

var editText:String = "hello world"

textview.setText(name)
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • Please, use code blocks for share your code – SerjantArbuz Dec 15 '22 at 14:59
  • Please don't post code-only answers. The main audience, future readers, will be grateful to see explained *why* this answers the question instead of having to infer it from the code. Also, since this is an old, well answered question, please explain how it complements all other answers. – Gert Arnold Dec 16 '22 at 21:53
-4

Look at the API of EditText:

void setText (CharSequence text, TextView.BufferType type)

Editable getText ()

Link: https://developer.android.com/reference/android/widget/EditText.html

Wenris
  • 226
  • 2
  • 9
-6

Try using nametxt.post: nametxt.post({nametxt.setText("your text")})

Denys Lobur
  • 124
  • 5