Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we hide include layout programmatically in Android?

I have to include one layout in my application. So that I have used

<include     android:id="@+id/support_layout"     android:width="match_parent"     android:height="match_parent"     layout="@layout/support"/> 

I have referenced this include tag in my java file using View.

View v = (View) findViewById(R.id.support_layout); 

But at some point of my code I have to Hide this layout. so that I used v.GONE

But it's not Hiding. I want to reference those text and button attributes located in XML programatically. How can I do that?

There is my support.xml:

<LinearLayout     android:id="@+id/support_layout"     android:width="match_parent"     android:height="match_parent">      <TextView         android:id="@+id/txt"         android:width="match_parent"         android:height="wrap_content"/>      <Button         android:id="@+id/btn"          android:width="match_parent"         android:height="wrap_content"         android:text="Button"/>  </LinearLayout> 
like image 237
Dharma Sai Seerapu Avatar asked Nov 20 '14 05:11

Dharma Sai Seerapu


People also ask

How do you hide a linear layout?

setVisibility(View. INVISIBLE); flag = false; } else{ your_linear_layout. setVisibility(View. VISIBLE) flag = true; } } };

Is it possible to include one layout definition in another?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.


1 Answers

Since <include/> is not a View type in android and visibility is the property of View, we can not access the visibility from included layout's reference.

However if you are using kotlin with view binding, we can get the reference of root of the included layout like binding.supportLayout.root which probably will be one of the View (ConstraintLayout, RelativeLayout, LinearLayout etc.)

Now we have reference of view means we can play with their visibility like below code.

binding.supportLayout.root.visibility = View.GONE 

Hope you got the idea.

like image 128
Ashish M Avatar answered Sep 17 '22 12:09

Ashish M