Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewBinding - How to set visibility for include layout in ViewBinding?

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?

like image 563
Parisa Baastani Avatar asked Apr 27 '20 06:04

Parisa Baastani


People also ask

How do I use layouts in view binding?

To enable view binding, configure viewBinding in your module-level build. gradle file. Once enabled for a project, view binding will generate a binding class for all of your layouts automatically. You don't have to make changes to your XML — it'll automatically work with your existing layouts.

How do I get view from view binding?

If you want to disable the view binding specific layout then you need to add tools:viewBindingIgnore=”true” to the root view of the layout file. After enabling it, we can start using it right away and all the binding classes are generated by default when you finish syncing your build. gradle file.

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.

What is viewbinding and how do I use it?

The idea behind ViewBinding from the Android team is to create a binding class for every layout that holds the references of the views in the layout. This reduces boilerplate code in your component (activity and fragment), and we can access views by creating an instance to that class, which you’re going to learn shortly.

How do I bind an view to a layout?

View binding can be used with the <include> layout tag. There are usually two kinds of <include> tags included in layouts, with or without <merge>. <include> without <merge>. We need to assign <include> an ID and then use that ID to access the view inside the included layout.

What is the difference between view binding and findviewbyid?

An instance of a binding class contains direct references to all views that have an ID in the corresponding layout. In most cases, view binding replaces findViewById. View binding is enabled on a module by module basis.

How to enable view binding in a module in Android?

To enable view binding in a module, you need to add the following lines under the android tag in the module level build.gradle file: After enabling view binding, whenever we create a new layout, a binding file with reference to the views in the layout is generated.


Video Answer


1 Answers

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.

like image 68
Somesh Kumar Avatar answered Sep 30 '22 18:09

Somesh Kumar