Home > Software engineering >  What is the meaning of these keywords in Android ViewBinding?
What is the meaning of these keywords in Android ViewBinding?

Time:09-19

lateinit keyword is used for late initialization of variables. -->Var is used for creating variable -->binding is a variable name -->What is the meaning of this (: ActivityMainBinding) Some people said that is class and some said that created an object, I am too confused. -->Again write the variable name binding and initialize the ActivityMainBinding.inflate(layoutInflater) -->What is the meaning of these keywords ActivityMainBinding.inflate(layoutInflater)-->Last Question is what is the meaning of this setContentView(binding.root)

lateinit var binding: ActivityMainBinding binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root)

CodePudding user response:

So binding is used to bind your views.

lateinit var binding :ActivityMainBinding ->here :ActivityMainBinding is a generated class which contains layout ids etc.

binding = ActivityMainBinding.inflate(layoutInflater) -> here we are inflating layout.

setContentView(binding.root)-> here we are setting our layout with class when class runs.Means here we are setting what we see when this activity runs.

CodePudding user response:

ActivityMainBinding=This class is auto generated by system using your xmlName Binding Keyword

ActivityMainBinding.inflate(layoutInflater)=It inflate the view hierarchy and bind the object to it.

binding.root=To Get a reference to the root view by calling the getRoot() method and pass this root view in setContentView() method to attach that root view to Activity

Check these links for details https://developer.android.com/topic/libraries/data-binding/generated-binding

How to use view binding in Android

  • Related