Alonso Alonso - 5 months ago 31
Android Question

Why ImageView is NOT VISIBLE in Layout?

I want to make an image appears as VISIBLE in the layout by programming it.

This is the XML file where I declare the image as GONE for then make it VISIBLE from code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/map"
tools:context="dis2.widget.MainActivity">

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:id="@+id/contactsScrollView"
android:fillViewport="false"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="26dp">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">

<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/linkg"
android:id="@+id/linkgID"
android:visibility="gone"/>

</LinearLayout>
</HorizontalScrollView>

</RelativeLayout>


And here is the MainActivity Java file:

private greenBeeper g;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageView b = (ImageView) findViewById(R.id.linkgID);
b.setVisibility(View.VISIBLE);
}

Answer

You have made your LinearLayout as well as ImageView gone in xml. While in java you are only making your image visible and your imageView parent is still in gone state. That is the problem in your code. Give some id to your linear layout too and make it visible when you want your imageview to be visible