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 322 times
2 Answers
0
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
- 26
- 3
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
- 41
- 1
- 5