Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide all elements in a Layout in Android at once?

I'm developing an Android app in eclipse.

I have an XML which has 2 linear layouts next to each other. The left one has several buttons, which make content visible in the right. However, I would not only like these buttons to make specific content visible, but hide (setVisibility.GONE) all the other stuff in that layout. I have already tried removeAllViews, but this is not suitable for me since it deletes them. So my idea is to hide (set the visibility to gone) every single stuff, then make the ones I wish visible. The reason I ask this is that it takes too much time setting the visibility to gone for everything (24, actually) for all the 12 buttons.

Thanks in advance

Layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/alap"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView3"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView4" >

        <LinearLayout
            android:layout_width="197dp"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/lewis" />

            <Button
                android:id="@+id/first"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/first" />

            <Button
                android:id="@+id/second"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/second" />

        </LinearLayout>
    </ScrollView>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:scaleType="center"
        android:src="@drawable/cpr1" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/epizodok" />

    <LinearLayout
        android:id="@+id/def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView3"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/scrollView2"
        android:layout_toRightOf="@+id/scrollView2"
        android:orientation="vertical"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/lewis"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" />

    </LinearLayout>

</RelativeLayout>
like image 800
BarniPro Avatar asked Dec 01 '22 21:12

BarniPro


2 Answers

Please try this...

<LinearLayout
        android:id="+id/ll"
        android:layout_width="197dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <!-- All content which you hide/show put here -->

</LinearLayout>

now in your class file you read this.

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

after that as per your requirement you hide/show this Layout with their all contents.

like: For Hide.

ll.setVisibility(View.GONE);

For Show.

ll.setVisibility(View.VISIBLE);

when you hide/show LinearLayout all content with this Layout also hide/show automatically.

like image 138
Mr.Sandy Avatar answered Dec 10 '22 13:12

Mr.Sandy


Simply, Just take another Linear Layout with vertical layout within your "def" Linear layout..Nd change its visibility as per your need.

<LinearLayout
        android:id="@+id/def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView3"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/scrollView2"
        android:layout_toRightOf="@+id/scrollView2"
        android:orientation="vertical"
        android:visibility="visible" >
     <LinearLayout
        android:id="@+id/Subdef"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/lewis"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" />
</LinearLayout>
    </LinearLayout>
like image 22
Bhoomika Brahmbhatt Avatar answered Dec 10 '22 11:12

Bhoomika Brahmbhatt