4

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>
BarniPro
  • 133
  • 1
  • 2
  • 10
  • share your layout code... – Pankaj Kumar Jun 25 '13 at 08:35
  • read LinearLayout id from Activity for `@+id/def` and then set visiblity of this layout. This will show/ hide all views into this layout. – Pankaj Kumar Jun 25 '13 at 08:42
  • Could you please turn it into a code for me? the id is "def". Won't it hide the layout? – BarniPro Jun 25 '13 at 08:44
  • @PankajKumar I have managed to write a code `code`View def = (View) findViewById(R.id.def); def.setVisibility(View.GONE);`code` but this hides the whole layout as I expected, but I would only like to hide all the children, not the layout itself. – BarniPro Jun 25 '13 at 08:57
  • You are right. Here you can get help http://stackoverflow.com/questions/7068873/how-can-i-disable-all-views-inside-the-layout – Pankaj Kumar Jun 25 '13 at 09:06

3 Answers3

9

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.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
6

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>
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
5

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.

Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69
  • Thanks for your answer, the only problem I had is that the user may not wish to go to the next one. I used sublayouts – BarniPro Jun 26 '13 at 10:06