How to change visibility of layout programmatically

AndroidLayoutVisibility

Android Problem Overview


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>

Android Solutions


Solution 1 - Android

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.)

Solution 2 - Android

TextView view = (TextView) findViewById(R.id.textView);
view.setText("Add your text here");
view.setVisibility(View.VISIBLE);

Solution 3 - Android

Use this Layout in your xml file

<LinearLayout
  android:id="@+id/contacts_type"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:visibility="gone">
</LinearLayout>

Define your layout in .class file

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.contacts_type);

Now if you want to display this layout just write

 linearLayout.setVisibility(View.VISIBLE);

and if you want to hide layout just write

 linearLayout.setVisibility(View.INVISIBLE);

Solution 4 - Android

You can change layout visibility just in the same way as for regular view. Use setVisibility(View.GONE) etc. All layouts are just Views, they have View as their parent.

Solution 5 - Android

this is a programatical approach:

 view.setVisibility(View.GONE); //For GONE
 view.setVisibility(View.INVISIBLE); //For INVISIBLE
 view.setVisibility(View.VISIBLE); //For VISIBLE

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDariusz BacinskiView Question on Stackoverflow
Solution 1 - AndroidaioobeView Answer on Stackoverflow
Solution 2 - AndroidSuperNova1054View Answer on Stackoverflow
Solution 3 - AndroidpavelView Answer on Stackoverflow
Solution 4 - AndroidKonstantin BurovView Answer on Stackoverflow
Solution 5 - AndroidHanishaView Answer on Stackoverflow