Eddi
Eddi

Reputation: 577

Android Kotlin ImageView on top of each other, one is hidden with android:visibility="gone" when both switch visibility second image is always blank?

Here's my layout

          <ImageView
              android:id="@+id/my_img1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>

          <ImageView
              android:id="@+id/my_img2"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:visibility="gone"/>

Both ImageView were loaded with glide

Glide.with(MyImg1).load(Img1Url).into(MyImg1)
Glide.with(MyImg2).load(Img2Url).into(MyImg2)

When user click a button I just switch their visibility

    if (MyImg1.visibility == View.VISIBLE) {
        MyImg1.visibility = View.INVISIBLE
        MyImg2.visibility = View.VISIBLE
    }
    else {
        MyImg1.visibility = View.VISIBLE
        MyImg2.visibility = View.INVISIBLE
    }

Now the problem is MyImg2 will always be blank no matter how many times the switch button was click. If I change android:visibility="gone" from my_img2 and put it to my_img1 then MyImg2 will show and this means image loading is fine.

Upvotes: 0

Views: 276

Answers (2)

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

Instead of View.INVISIBLE try with View.GONE

if (MyImg1.visibility == View.VISIBLE) {
        MyImg1.visibility = View.GONE
        MyImg2.visibility = View.VISIBLE
    }
    else {
        MyImg1.visibility = View.VISIBLE
        MyImg2.visibility = View.GONE
    }

Upvotes: 1

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2728

If you are using RelativeLayout as the parent for both of them then add android:layoutparentBelow = "@+id/my_img_1" In your second imageview layout file

Upvotes: 0

Related Questions