Q: Image doesn't turn invisible I made 2 ImageViews on top of each other. I want to toggle the image on top visible and invisible when I click on the checkbox. But it doesn't turn back into invisible once it became visible. I tried to use View.GONE too, but then it dissappears without getting back to visible.
Here my Code
    val btn_click_me = findViewById(R.id.checkBox) as Button
    btn_click_me.setOnClickListener {
        val imageon = findViewById(R.id.imageViewOn) as ImageView
        if(imageon.visibility == View.VISIBLE) {
            imageon.setVisibility(View.INVISIBLE)
        }
        if(imageon.visibility == View.INVISIBLE){
            imageon.visibility = View.VISIBLE
        }
    }

my xml code:
<ImageView
    android:id="@+id/imageViewOff"
    android:layout_width="37dp"
    android:layout_height="34dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:visibility="visible"
    app:srcCompat="@drawable/haltestelleoff" />

<ImageView
    android:id="@+id/imageViewOn"
    android:layout_width="37dp"
    android:layout_height="34dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:visibility="invisible"
    app:srcCompat="@drawable/haltestelleon" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toStartOf="@id/imageViewOff"
    android:text="" />

my xml layout is Relative Layout.

A: Please check the condition.It should be if else and not if if condition.
  if(imageon.visibility == View.VISIBLE) {
        imageon.setVisibility(View.INVISIBLE)
    }else {
        imageon.setVisibility(View.VISIBLE)
    }


A: look at here :
if(imageon.visibility == View.VISIBLE) {
        imageon.setVisibility(View.INVISIBLE)
}
if(imageon.visibility == View.INVISIBLE){
        imageon.visibility = View.VISIBLE
}

Here first you make it invisible and in the second if you check and make it visible again !
change it to if else
