ViewBinding in Android. View binding, information binding… | by way of Eren Mollaoğlu | Feb, 2024

View binding, information binding, findViewById, encode the background of perspectives.

In android now we have perspectives for have interaction with consumer. Sure now we have, but when we wish to do a little movements when have interaction consumer with view, then we will have to use viewBinding, dataBinding or findViewById. They’re supply us to encode the background of the view.

View binding is a function that makes it more uncomplicated to put in writing code that interacts with perspectives.

In android each view have personal ID, findViewById command take a look at to achieve view with identification and encode the background of the view.

information Binding is attempt to move our serve as title to structure for the view.

View Binding vs findViewById;

  • Null protection: if invalid identification handed to findViewById this reasons the null pointer exception, Then again view binding creates direct referances to perspectives, there’s no possibility of a null pointer exception.
  • Sort protection: if two perspectives have the similar identification and we move that identification to findViewById, the referenced view varieties can’t fit, which is able to reason a category solid exception.

View Binding vs Information Binding;

  • View binding calls for no annotation processing, so bring together time are sooner.
  • Principally view binding most effective bind perspectives to code, whilst information binding bind information from code to perspectives.
  • View binding doesn’t require specifically tagged XML structure recordsdata, so it’s sooner to undertake on your apps. If you permit view binding in module, it applies all of that module’s layouts routinely, however if you wish to use information binding you will have to tagged XML structure recordsdata.

Setup

android {
...
buildFeatures {
viewBinding = true
}
}

You will have to set view binding true from the construct gradle on module stage. Then on our fragment or process web page we will have to bind structure to a variable;

//For Job
non-public lateinit var binding: ResultProfileBinding

override amusing onCreate(savedInstanceState: Package?) {
tremendous.onCreate(savedInstanceState)
binding = ResultProfileBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}

//For Fragment
non-public lateinit var binding: FragmentCalcGeneralBinding
override amusing onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Package?
): View? {
binding = FragmentCalcGeneralBinding.inflate(inflater, R.structure.fragment_calc_general, container,false)
go back binding.root
}

Then we will be able to use each view inside specified structure;

binding.viewName.attributeName = doSomething
//Pattern
binding.textView.textual content = "Hi"
binding.button.setOnClickListener{
Toast.makeText(requiredContext(),"Button pressed",Toast.LENGTH_LONG).display()
}

I can have errors, I make an apology if I’ve errors, if you happen to suppose I’ve errors, you’ll be able to touch me. Thanks excited about studying, Satisfied coding…

My Social Accounts;

And in addition you’ll be able to achieve me in eren.mollaoglu@outlook.com mail deal with.

Leave a Comment

Your email address will not be published. Required fields are marked *