android – How to set visibility in Kotlin?-ThrowExceptions

Exception or error:

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);
    }
}
How to solve:

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
    }
}

###

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

###

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()

###

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
        }
    }

###

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()
}

###

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)
}

###

A simple way in Kotlin:

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

###

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
    }

Leave a Reply

Your email address will not be published. Required fields are marked *