Search code examples
androidkotlintextview

How to update TextView text after clicking a button


when i cick on number the TextView Doesnt change it supposed to show number when i click on them like the calculator that's my code i still in the first part of the app

 fun clicknumber(view: View) {

        var buselected = view as Button
        var tvnum = tv.text.toString()
       if (newOp){
            tv.text = ""
       }
        newOp=false


        when (buselected.id) {
           one.id-> tvnum += "1"
            two.id-> tvnum += "2"
            three.id-> tvnum += "3"
            four.id-> tvnum += "4"
            five.id-> tvnum += "5"
            six.id-> tvnum += "6"
            seven.id-> tvnum += "7"
            eight.id-> tvnum += +"8"
            nine.id-> tvnum += "9"
            dot.id-> tvnum += "."
            minusplus.id -> tvnum = "-" + tvnum
        }
    }
}

i think the problem is in the "tvnum" and i wanna to clear the textview when i do a new operation, i write the code but it doesnt work so good


Solution

  • You're assigningtvnum but you're doing nothing with it. You need to set the text in the TextView instead. tv.text = tvnum