Search code examples
android-studiokotlintextview

Setting a text view to blank text after being toggled


I have a text view that is clickable. Every time the user clicks the text box, it will display a string. When they click it again, the box will become blank. I have this so far:

 textToggle.setOnClickListener {
        textToggle.text = "Hello"
    }

I have seen some examples where the person did an override on onClick. Would that be the case for here too, or is there a simpler way that I'm not seeing?


Solution

  • Just check the length of the text when you click on it.

    textToggle.setOnClickListener {
       if(textToggle.text.length > 0) textToggle.text = ""
       else textToggle.text = "Hello"
    }