10

I use an include layout and I need to change it's visibility:

 <include
    android:id="@+id/layout_provinces"
    layout="@layout/layout_select_provinces"
     />

and layout_select_provinces is sth like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/select_province"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@color/white"
 android:visibility="gone">

    <LinearLayout
        android:id="@+id/layout_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background_color"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_state"
            style="@style/FontIranBold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/_8sdp"
            android:layout_marginRight="@dimen/_4sdp"
            android:layout_marginBottom="@dimen/_8sdp"
            android:text="@string/txt_select_province"
            android:textSize="@dimen/font_size_small" />

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/_1sdp"
            android:background="@color/gray_special" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_estate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout_top"
        android:layout_marginTop="@dimen/_8sdp" />

but when I set Id to RelativeLayout, my app crashes and I can't change the visibility:

     binding.layoutProvinces.selectProvince.setVisibility(View.GONE);

is there anyone can help me with ViewBinding set id process?

Parisa Baastani
  • 1,713
  • 14
  • 31
  • I think I know what you are doing wrong.. but first, add the layout XML `layout_select_provinces` code you are adding in `` – Somesh Kumar Apr 28 '20 at 05:14
  • Please check again, I had a misspelling. the middle code is for layout_select_province. the problem is I can't set Id for include tag (I mean "layout_provinces") and for RelativeLayout ( I mean "select_province"), the app crashes, but I need both of them, the first id is needed for ViewBinding and the second Id is needed to setVisibility. – Parisa Baastani Apr 28 '20 at 06:30

2 Answers2

33

When we give <include> an id it overrides the id of root layout that we included. In your case, layout_provinces is overriding <RelativeLayout's select_province. So when you access the relative layout it gives you an error because it is no longer there.

What you can do is remove the id of relative layout and only give id to include and access the root layout that you included through the getRoot() method like below.

Java

binding.selectProvince.getRoot().setVisibility(View.GONE)

Kotlin

binding.selectProvince.root.visibility = View.GONE

the getRoot() method in View Binding returns the topmost view of the given layout in your case it's the RelativeLayout.

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
  • Shouldn't this be `binding.layoutProvinces.root.visibility = View.GONE` because you said to "remove the id of relative layout" which is `select_province` "only give id to include" which is `layout_provinces`? – Just The Highlights Dec 18 '21 at 19:24
-1

For DataBinding to work, you need to have <layout> tag as your root tag. So wrap your xml code inside <layout> like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
     android:id="@+id/select_province"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@color/white"
     android:visibility="gone">

        <LinearLayout
            android:id="@+id/layout_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_color"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txt_state"
                style="@style/FontIranBold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/_8sdp"
                android:layout_marginRight="@dimen/_4sdp"
                android:layout_marginBottom="@dimen/_8sdp"
                android:text="@string/txt_select_province"
                android:textSize="@dimen/font_size_small" />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/_1sdp"
                android:background="@color/gray_special" />

        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_estate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/layout_top"
            android:layout_marginTop="@dimen/_8sdp" />

    </RelativeLayout>

</layout>
arvind
  • 104
  • 4