Android View Binding -How to use in different scenarios
Before diving into the scenario lets understand the View Binding
View Binding allow you to easily and efficiently write code that interact with view.Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.
- Its batter than the findviewbyid approach as it have Null Safety ,Type Safety advantages.
- Its batter than Data Binding as its faster in compiling the application and easy to use.
Setup
To use View Binding in you application you first need to add below line into your build.gradle file in android block.
android {
buildFeatures {
viewBinding true
}
}
Once you added this line in your project after that rebuild your project that will generate class for each xml file that you have in your project.
If you have activity_main.xml file in your layout file it will generate ActivityMainBinding class for this xml file and so on for fragments and other layouts.
MainActivity.kt(Activity File)
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
You can now use the instance of the binding class to reference any of the views:
binding.name.text = "Hello World"
binding.button.setOnClickListener { }
Using Viewbinding in Fragment
Same as Activity its binding class will be generated base on the name of its xml file.
private var _binding: SignInFragmentBinding? = null
// This property is only valid between onCreateView and
// onDestroyView as we are assigning null in ondestoryview.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = SignInFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null //clear the referance by assigning null
}
View Binding in RecyclerView Adapter
To use viewbinding in recyclerview you need to create binding object in oncreateviewholder and pass that into your viewholder then while calling onbindviewholder you can use that value.
class UserAdapter(var datalist:List<Data>):RecyclerView.Adapter<UserAdapter.UserHolder>() {
//Pass your binding object from oncreateviewholder to here
class UserHolder(val binding: ItemRecylerBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(data: Data) {
binding.email.text = data.email
binding.name.text = data.name
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder {
//created binding object
val binding = ItemRecylerBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return UserHolder(binding)//passed binding to viewholder
}
override fun onBindViewHolder(holder: UserHolder, position: Int) {
holder.bind(datalist.get(position))
}
override fun getItemCount(): Int {
return datalist.size
}
}
Using Viewbinding in Dialog
You can use below code to use viewbinidng in dialog
val bind :CustomDialogBinding = CustomDialogBinding .inflate(inflater)
dialog.setContentView(bind.root)
Drop a Comment if you have any questions.
Happy Coding…. (:})