Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewBinding - Included Layout Binding Resulting in Unresolved Reference

Tags:

I am implementing ViewBinding in one of my fragments. This fragment has a layout included like so:

...
<androidx.core.widget.NestedScrollView
        android:id="@+id/sv_sudf_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_sudf_continue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/eav_sudf_avatar">

        <include
            android:id="@+id/l_sudf_details"
            layout="@layout/layout_sign_up_details_fields"/>

    </androidx.core.widget.NestedScrollView>
...

I have followed this answer but it also does not work.

The generated view binding class for the fragment has the binding inside, however, the type for the attribute is View. When I then reference the View using binding.lSudfDetails the type is LayoutSignUpDetailFieldsBinding. Where this type is coming from I can't work out as there is no generated class with that name however, I would expect it would assign it the proper binding type. Here is the attribute in the FragmentSignUpDetailsBinding.java.

@NonNull
public final View lSudfDetails;

The bindings are all correctly setup however and it allow me to reference views within the nested layout but when I come to build I get unresolved reference errors. Lint does not complain when I reference them like this:

binding.lSudfDetails.etSudfDob

The compiler does fail however with errors such as this

Unresolved reference: etSudfDob

The binding itself is created according to the Android docs:

private var _binding : FragmentSignUpDetailsBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentSignUpDetailsBinding.inflate(inflater,container,false)
        return binding.root
    }

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    binding.tvSudfWelcome.text = getString(R.string.sign_up_welcome,getString(R.string.app_name))
    binding.lSudfDetails.etSudfDob.setOnClickListener {
            showYearSelection()
    }
}

The tvSudfWelcome binding works its the nested binding it doesn't like.

like image 712
StuStirling Avatar asked Mar 05 '20 10:03

StuStirling


People also ask

What is ViewBinding?

ViewBinding is introduced in the Gradle version 3.6 and above (which comes with the Android Studio 4.0, only gradle 3.6). ViewBinding also helps to reduce the boilerplate code, hence reducing the code redundancy.

How would you enable view binding for a module in your application so you can reference views from layout files without using Findviewbyid?

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.

What is ActivityMainBinding in Android?

xml so the corresponding generated class is ActivityMainBinding . This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.

What is data binding and view binding in Android?

The one and only function of View Binding is to bind the views in the code, while Data Binding offers some more options like Binding Expressions, which allows us to write expressions the connect variables to the views in the layout.


1 Answers

If you're using Android Studio 3.6.0 sometimes gradle plugin fails to generate ViewBinding fields for included layouts. Please update to Android Studio 3.6.1 and gradle plugin version to 3.6.1.

like image 175
Somesh Kumar Avatar answered Sep 30 '22 20:09

Somesh Kumar