i was told to set dataBindingUtil in build.gradle after doing that i am being told to use dataBindingUtil to inflate every layout not sure how to use dataBindingUtil to inflate every layout
Asked
Active
Viewed 1,211 times
0
-
you can find the **data binding** in [here](https://developer.android.com/topic/libraries/data-binding). You may find also very interesting its related but diferrent **view binding** in [here](https://developer.android.com/topic/libraries/view-binding) that looks closer to what you seem to look for. – Hernanibus Jun 15 '22 at 02:32
2 Answers
1
You can use dataBinding alongside viewBinding to declare your view hassle-free in your Kotlin/Java file. In your app build.gradle file add the following
android {
......
buildFeatures {
dataBinding true
viewBinding true
}
}
Now convert the xml layout you want to use data binding in to data binding layout by wrapping it in
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<ConstraintLayout... /> <!-- UI layout's root element -->
In order to inflate and use this variable in your Kotlin file, add the following line
ViewNameBinding binding = ViewNameBinding.inflate(layoutInflater)
Here ViewNameBinding is auto generate as per you view name.

Hyzam
- 143
- 8
0
Use Binding class's inflate
as recommended in Android Documentation.
DataBindingUtil used when only you don't have generated binding class.
You can generated binding class, use that class instead of using DataBindingUtil.
In Kotlin
lateinit var binding: HomeFragmentBinding
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = HomeFragmentBinding.inflate(inflater, container, false)
return binding.root
}
If your layout biniding class is not generated @See this answer.

Eko S. Purnomo
- 51
- 1
- 6