BarniPro
BarniPro

Reputation: 163

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>

Upvotes: 4

Views: 13363

Answers (3)

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

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>

Upvotes: 6

Mr.Sandy
Mr.Sandy

Reputation: 4349

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.

Upvotes: 9

Raghu Mudem
Raghu Mudem

Reputation: 6963

No need to hide views instead of that just use ViewFlipper concept. Just declare view flipper in your xml file with your two linearlayouts like this

    <Viewflipper android:id="@+id/myViewFlipper" android:layout_height="wrap_content" android:layout_width="match_parent">
    <LinearLayout android:id="@+id/layout1" android:layout_height="wrap_content" android:layout_width="match_parent"></LinearLayout>
    <LinearLayout android:id="@+id/layout2" android:layout_height="wrap_content" android:layout_width="match_parent"></LinearLayout>
   </Viewflipper>

And get viewflipper reference in your java code like this

Viewflipper mFlipper = (Viewflipper)findViewById(R.id.myViewFlipper);

use mFlipper.showNext(), use mFlipper.showPrevious() methods to hide and show your layouts thats it.

Upvotes: 5

Related Questions