Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding linear layout on runtime in Android

I am having following layout

<merge>
    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        />
    <LinearLayout
        android:id="@+id/ll_sub"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        />  
</merge>

What I want to do is to show/hide the ll_sub layout on runtime through setVisibility() but it is not working.

When I am setting android:visibility="gone" (also I had checked with invisible) from the xml of ll_sub then it is not displayed on the screen and this time when I use setVisibility() to show this layout on runtime, it is displayed but when I try to hide this layout once it is displayed then it is not hiding.

EDIT

I am trying to show/hide this linear layout on click of a button.

LinearLayout ll;
Button minimize;
int visibility=0;

@Override
public void onCreate(Bundle savedInstanceState)
{
    ll=(LinearLayout)findViewById(R.id.ll_sub);
    minimize=(Button)findViewById(R.id.minimize);
    minimize.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {

            if(visibility==0)
            {
                visibility=2;
            }
            else
            {
                visibility=0;
            }
            ll.setVisibility(visibility);

        }
    });
}
like image 440
Vinod Maurya Avatar asked Apr 13 '11 04:04

Vinod Maurya


People also ask

How do I turn off linear layout?

I am able to disable all content inside linear layout using following code: LinearLayout myLayout = (LinearLayout) findViewById(R. id. linearLayout1); for ( int i = 0; i < myLayout.

How do you hide layout?

You can also set the visibility in your layout. xml if you want it hidden when your application first starts. android:visibility="gone" should do the trick. This way it is hidden from the very start when the layout is initialized by your app.

How do I stop nested weights on android?

You could use RelativeLayout to avoid the nested weights. Nested weights are bad for performance because the number of measurements increase exponentially with each one nested.


2 Answers

It looks like you're setting the wrong constants for changing view visibility.

GONE == 8
INVISIBLE == 4
VISIBLE == 0

However, you should never rely on the actual values that Android happened to designate to represent their constants. Instead use the the values defined in the View class: View.VISIBLE, View.INVISIBLE, and View.GONE.

// snip...
if(visibility == View.VISIBLE)
{
    visibility = View.GONE;
}
else
{
    visibility = View.VISIBLE;
}
ll.setVisibility(visibility);

And don't forget to call invalidate() on the view :)

like image 192
skabbes Avatar answered Nov 05 '22 11:11

skabbes


You should use the Constants provided by View

View.INVISBLE, View.VISIBLE, View.GONE

and also invalidate your View

like image 31
Hache Avatar answered Nov 05 '22 12:11

Hache