Skip to content
Advertisement

imageView.setVisibility(View.GONE); not working for ImageView

  • I am trying to implement imageView.setVisibility(View.GONE); if my URL is null then the visibility of the imageView should ne GONE or else it should be VISIBLE.
  • In my XML I have defined it as android:visibility="gone" and if the URL is not null then I am displaying the image.
  • The issue here is, it displays the image when it contains the url. But when the url is null it is giving the empty space.
  • XML
<ImageView
            android:id = "@+id/imageView"
            android:layout_width="200dp"
            android:layout_height="300dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:layout_gravity="center"
            android:visibility="gone"
            android:src="@drawable/rectanglebutton"/>
  • Java
url = list.get(position).getImageUrl();
            if(url == "null") {
                imageView.setVisibility(View.GONE);
                imageView.requestLayout();
            }
            else
            {
                imageView.setVisibility(View.VISIBLE);
                imageView.requestLayout();
                Glide
                        .with(DisplayQuestions.this)
                        .load(url)
                        .into(imageView);
            }

Advertisement

Answer

the problem seems to be in the java code.

try changing

if (url == "null")

to

if (url == null)
  • null should not be in doubble quotes.
  • and generally you should use "some_string".equals(object) to test strings.
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement