Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you hide multiple views at once?

I have a RelativeLayout view, and 3 children's views too. I'm trying to hide them all in code by setting the relative layout to INVISIBLE using setVisibility. The funny thing is that when I use setVisibility(View.INIVISIBLE) only the first child is hidden, not the other two. So I'm a bit confused - if I set a parent view to invisible shouldn't it change the visibility of all the children or leave them all alone?

Feel free to point me to a reference page that explains it - I can't find anything.

Update: I've tried setting it to View.GONE, but the same thing happens, except the two children who remain visible move up a bit.

Here's the relevant XML:

<RelativeLayout
    android:id="@+id/optionsform"
    android:layout_width="fill_parent"
    android:padding="8dp"
    android:layout_height="wrap_content" >
    
    <TextView
        android:id="@+id/tvoptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/tvoptions"
        android:textColor="#f000"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="bold"/>
    
    <TextView
        android:id="@+id/tvdictionary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tvoptions"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="16dp"
        android:text="@string/dictionary"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#f000" />

    <Spinner
        android:id="@+id/dictionary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvdictionary"
        android:layout_alignParentRight="true"
        android:layout_marginTop="-10dp"
        android:layout_marginLeft="6dp"
        android:layout_toRightOf="@+id/tvdictionary" />

</RelativeLayout>

And here's the relevant code I'm using:

    public void onClick(View v) {
        //Toggle viewing of options, using "if" in case it is set to View.GONE 
        View view = findViewById(R.id.optionsform);
        if (view.getVisibility() == View.VISIBLE) 
            view.setVisibility(View.INVISIBLE);
        else
            view.setVisibility(View.VISIBLE);
    }
like image 855
Matt Parkins Avatar asked Nov 18 '11 16:11

Matt Parkins


3 Answers

Try to set all three views to View.INVISIBLE or to View.GONE.

OR

You can try

public void onClick(View v) {
    //Toggle viewing of options, using "if" in case it is set to View.GONE 
    RelativeLayout view = (RelativeLayout) findViewById(R.id.optionsform);
    if (view.getVisibility() == View.VISIBLE) 
        view.setVisibility(View.INVISIBLE);
    else
        view.setVisibility(View.VISIBLE);
}
like image 57
Frank Avatar answered Nov 12 '22 15:11

Frank


You must set to the View.GONE state.

like image 22
Jordi Coscolla Avatar answered Nov 12 '22 15:11

Jordi Coscolla


 <TextView
        android:id="@+id/tvdictionary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        **android:layout_below="@+id/tvoptions"** // *should  be  android:layout_below="@id/tvoptions*
        android:layout_marginLeft="30dp"
        android:layout_marginTop="16dp"
        android:text="@string/dictionary"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#f000" />



     <Spinner
            android:id="@+id/dictionary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            **android:layout_alignTop="@+id/tvdictionary"** // *should be android:layout_alignTop="@id/tvdictionary*
            android:layout_alignParentRight="true"
            android:layout_marginTop="-10dp"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@+id/tvdictionary"// *should be android:layout_toRightOf="@id/tvdictionary*
 />

@id is used while referencing layout id
@+id is used while creating new layout id

like image 33
Pratik Bhat Avatar answered Nov 12 '22 17:11

Pratik Bhat