View Binding Android

 Why ?

  • To replace boilerplate code of findViewById
  • Always type-safe and null-safe 

What ?
  • Enable view binding in build.gradle file without library dependencies 
  • View Binding generates a binding object for every layout in your module  (activity_main.xml = ActivityMainBinding.class)
Use case : 

buildFeatures {
viewBinding = true
}


// using View binding in Activity
binding = ActivityMainBinding.inflate(layoutInflater)

binding.progressBar.setVisibility(ProgressBar.INVISIBLE)

// Send button sends a message and clears the EditText
binding.sendButton.setOnClickListener {
// TODO: Send messages on click
// Clear input box
binding.messageEditText.setText("")
}

setContentView(binding.root)


































Comments

Popular Posts