'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() == View.VISIBLE) {
        view.setVisibility(View.INVISIBLE);
    } else {
        view.setVisibility(View.VISIBLE);
    }
}


Solution 1:[1]

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

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

Solution 2:[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:[3]

You can simply do it.

idTextview.isVisible = true
idTextview.isVisible = false

Solution 4:[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:[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:

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

Solution 6:[6]

You can use from bellow code:

fun View.isVisible(): Boolean {
    return visibility == View.VISIBLE
}

And:

fun View.setVisible(visible: Boolean) {
    visibility = if (visible) {
        View.VISIBLE
    } else {
        View.GONE
    }
}

And you can use:

if (text_view.isVisible()) {
    text_view.setVisible(false)
}

Solution 7:[7]

A simple way in Kotlin:

fun toggleView(view: View) {
    view.isVisible = !view.isVisible
}

Solution 8:[8]

If the View is visible initially, one can use the xor operator to toggle the visibility.

view.visibility = view.visibility.xor(View.GONE)

The correct - and more readable - way however is to use the inline var View.isVisible:

view.isVisible = !isVisible


inline var View.isVisible: Boolean
    get() = visibility == View.VISIBLE
    set(value) {
        visibility = if (value) View.VISIBLE else View.GONE
    }

Edit May 1 2022:

Android developers have added an extension androidx.core.view.ViewKt#isVisible to toggle visibility between View.VISIBLE and View.GONE. So use that rather.

Solution 9:[9]

This is how I handle view's visibility in Kotlin. These methods can be called on any subclass of View class. E.g. LinearLayout, TextView etc.

VISIBLE / GONE:

// @BindingAdapter("visibleOrGone")
fun View.visibleOrGone(visible: Boolean) {
    visibility = if(visible) View.VISIBLE else View.GONE
}

VISIBLE / INVISIBLE:

// @BindingAdapter("visibleOrInvisible")
fun View.visibleOrInvisible(visible: Boolean) {
    visibility = if(visible) View.VISIBLE else View.INVISIBLE
}

Databinding: Uncomment @BindingAdapter if you also want to use above methods with databinding.

<FrameLayout
    app:visibleOrGone="@{viewModel.visibleView}"
    ...
    />

or

<EditText
    app:visibleOrInvisible="@{viewModel.visibleView}"
    ...
    />

My ViewModel class looks like this:

class LoginViewModel {
    val visibleView = ObservableBoolean()
}

Solution 10:[10]

You can get the visibility state of a view with the command .isVisible

I´ll show you how.

   val menu: ConstraintLayout = findViewById(R.id.layMenu)

    if (menu.isVisible==false){
        //view is not visible
    } else {
        //view is visible
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Om Prakash Sharma
Solution 3 pisumathu
Solution 4 zsmb13
Solution 5 Cao Minh Vu
Solution 6 jo jo
Solution 7 Rajath
Solution 8
Solution 9 Hemant Kaushik
Solution 10 Thiago Silva