Alonso
Alonso

Reputation: 141

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

Upvotes: 0

Views: 1094

Answers (5)

Chirag Arora
Chirag Arora

Reputation: 816

Enable both linear layout and image view visibility.

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible">
        <ImageView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@drawable/board_out_icon"
            android:id="@+id/linkgID"
            android:visibility="visible"/>

    </LinearLayout>

Upvotes: 1

bhumika rijiya
bhumika rijiya

Reputation: 440

Try this, In your xml change visibility of LinearLayout "visible" instead of "gone" and you can see imageview in your layout.

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

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

    </LinearLayout>

I hope you solve your problem with this..!!

Upvotes: 1

Shahar
Shahar

Reputation: 3692

Your LinearLayout is stated as gone.

    <LinearLayout
        android:id="@+id/imageParent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="gone">

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

    </LinearLayout>

Try removing the android:visibility="gone" from the LinearLayout, and use the rest of you Java code.

Upvotes: 1

Vivek Mishra
Vivek Mishra

Reputation: 5705

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

Upvotes: 3

Mohammed Atif
Mohammed Atif

Reputation: 4513

private greenBeeper g;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    ImageView b = (ImageView) findViewById(R.id.linkgID);
    b.setVisibility(View.VISIBLE);
    layout.setVisibility(View.VISIBLE);
}

give id to LinearLayout too

Upvotes: -1

Related Questions