Search code examples
androidbuttonkotlinandroid-viewshow-hide

How to hide and show a view with the same button in Kotlin


For an android app I want to hide and show a view with the same button. It's probably a very basic understanding that I'm missing. So I would be really grateful for an explanation.

I've tried the code below, but it's only working once.

if (view1.isVisible){
        btnHideShow.setOnClickListener{
            view1.visibility = View.GONE
            if (view1.isGone) {
                btnHideShow.setOnClickListener {
                    view1.visibility = View.VISIBLE
                }
            }
        }
    }

Solution

  • Simplest way to achieve that

    btnHideShow.setOnClickListener{ view1.visibility = !view1.visibility }