How can we hide include layout programmatically in Android?

android, android-layout

Solution

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.

Problem

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> ```

Original source