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>
setVisibility(View. INVISIBLE); flag = false; } else{ your_linear_layout. setVisibility(View. VISIBLE) flag = true; } } };
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With