0

As title says:

I´d like to change the text inside an editText with a button click.

I can do it with TextViews by using this code:

TextView.text= "new text here"

but it doesnt work for EditText

I already tried something like this:

editText.setText("new text here")

Can some one please tell me the equivalent code for editTexts?

this is my code so far:

        dialog.setOnClickListener {
        var builder = AlertDialog.Builder(this)
        var inflater: LayoutInflater = layoutInflater
        var view : View = inflater.inflate(R.layout.dialog,null)
        builder.setView(view)
        builder.setTitle("categories")
        builder.setPositiveButton("Ver", { dialogInterface: DialogInterface, i: Int ->
            mainscreen.visibility = View.GONE
            ListView.visibility = View.VISIBLE
            listdogs.visibility=View.VISIBLE
            listdogs.adapter = adapterdogs
            editText?.setText("husky")
        })

Regards

  • Possible duplicate of [Setting text in EditText Kotlin](https://stackoverflow.com/questions/44493908/setting-text-in-edittext-kotlin) – b2mob Aug 13 '18 at 17:50

2 Answers2

0

You need editText?.setText("new text here") or editText!!.setText("new text here") if you are sure that this EditText is not null.

  • I tried that and it shows this: only safe(?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type EditText? – Gabo Walker Aug 13 '18 at 18:08
  • @GaboWalker is there any problem? –  Aug 13 '18 at 18:22
  • Im making some tests, im trying to use your code in a dialog, I´m using this: editText!!.setText("new text here") right now, and its crashing my app, I´ll inform you what appened as soon as i check everything else is ok – Gabo Walker Aug 13 '18 at 18:34
  • If it's crashing your app then `editText` is `null` and you cannot use `setText` –  Aug 13 '18 at 18:44
  • With editText!!.setText("new text here") my app crashes, with: editText?.setText("new text here") my app doesnt crash but the editText keeps showing the hint: "Search" an nothing else. what I´m trying to achieve is to show a popup window, that when the user clicks on ok button, the app will take you to an specific cathegory and input a word in an editText, the one that is used to filter among a list. – Gabo Walker Aug 13 '18 at 18:45
  • After you inflate the layout to the dialog you must use findViewById to get the EditText –  Aug 13 '18 at 18:47
  • I added my code to my original question for you to see it – Gabo Walker Aug 13 '18 at 18:58
  • @GaboWalker Do you understand the [difference between !! and ?](https://kotlinlang.org/docs/reference/null-safety.html) ? Your edit text is null. With !! it crashes because you can't set text on null. With ? it does nothing because it only does something when `editText` is not null. You need to set `editText` to something first. – Eugen Pechanec Aug 13 '18 at 19:01
  • Thanks for your asistance Eugen Pechanec, that´s what I was asking in the very first place, how can I input some text to an EditText that is empty, after clicking a button. Let me check the link you shared, regards. – Gabo Walker Aug 13 '18 at 19:10
0

This is happening because java getter setter ambiguous in case of EditText, To understand this scenario you first need to understand that how kotlin generate the properties from java class for kotlin.

Kotlin official

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. Boolean accessor methods (where the name of the getter starts with is and the name of the setter starts with set) are represented as properties which have the same name as the getter method.

But in case EditText, which inherits setter from TextView creating problem here.

When a setter comes into play, property generation process becomes ambiguous. The reason is that the getter and the setter may have different types. Moreover, the getter and/or the setter may be overridden in a subclass, which exactly is the case of EditText in Android.

It 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. Considering that the String Class is not editable, I cannot assign a String instance to the text property of the EditText class.

Original source https://medium.com/cashify-engineering/how-does-kotlin-generated-property-from-java-getters-and-setters-undocumented-by-jetbrains-7e1ad88052b1

Moinkhan
  • 12,732
  • 5
  • 48
  • 65