Skip to content Skip to sidebar Skip to footer

How To Set Visibility In Kotlin?

I am new in Kotlin. I have a view that I need to show or hide in conditional ways. How can I do this in Kotlin? In Java: public void showHide(View view){ if (view.getVisibility

Solution 1:

In response to this answer, I believe a Kotlin-styled way to accomplish this can also be written as:

funshowHide(view:View) {
    view.visibility = if (view.visibility == View.VISIBLE){
        View.INVISIBLE
    } else{
        View.VISIBLE
    }
}

Solution 2:

if you want to visible icon

ic_back.visibility = View.VISIBLE

and if you want to visibility GONE so please try it :

ic_back.visibility = View.GONE

Solution 3:

You can simply do it.

idTextview.isVisible = trueidTextview.isVisible = false

Solution 4:

You could do this in an extension function:

fun View.toggleVisibility() {
    if (visibility == View.VISIBLE) {
        visibility = View.INVISIBLE
    } else {
        visibility = View.VISIBLE
    }
}

Can be used like this:

someView.toggleVisibility()

Solution 5:

You can convert using Android Studio: Click on the Java file you want to convert, choose Code -> Convert Java File To Kotlin File and see the magic. The result is:

funshowHide(view: View) {
        if (view.visibility == View.VISIBLE) {
            view.visibility = View.INVISIBLE
        } else {
            view.visibility = View.VISIBLE
        }
    }

Post a Comment for "How To Set Visibility In Kotlin?"