Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change visibility of layout programmatically

There is the way to change visibility of View, but how can I change programmatically visibility of layout defined in XML? How to get layout object?

<LinearLayout     android:id="@+id/contacts_type"     android:orientation="horizontal"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:visibility="gone"> </LinearLayout> 
like image 642
Dariusz Bacinski Avatar asked Aug 12 '10 08:08

Dariusz Bacinski


People also ask

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 change to linear layout?

Go to -> Edit File Templates option. Change the ${ROOT_TAG} to LinearLayout in both of these LayoutResourceFile. xml and LayoutResourceFile_vertical. xml and you will always get LinearLayout as parent for future.


1 Answers

Have a look at View.setVisibility(View.GONE / View.VISIBLE / View.INVISIBLE).

From the API docs:

public void setVisibility(int visibility)

    Since: API Level 1

    Set the enabled state of this view.
    Related XML Attributes: android:visibility

Parameters:
visibility     One of VISIBLE, INVISIBLE, or GONE.

Note that LinearLayout is a ViewGroup which in turn is a View. That is, you may very well call, for instance, myLinearLayout.setVisibility(View.VISIBLE).

This makes sense. If you have any experience with AWT/Swing, you'll recognize it from the relation between Container and Component. (A Container is a Component.)

like image 178
aioobe Avatar answered Sep 28 '22 10:09

aioobe