Today, you’ll learn how to link views with the code using view binding instead of legacy findViewById or a heavyweight data-binding framework. In my previous articles, we covered the basics. In this article, we’ll cover Android components like dialogs and adapters.

Introduction

For those who are new to view binding, it’s a lightweight library from the Jetpack family to link views with Android components. In the current Android environment, we have four solutions to link views with Android components:

  • findViewById — This is the legacy solution that involves more manual work.
  • Kotlin synthetics — This is a decent solution from Kotlin’s native library, but the Kotlin team recently deprecated it.
  • Data binding — This library offers much more advanced features than view linking, like two-way binding and more.
  • View binding — A native solution with null- and type-safe code.

If you’re interested in learning more about these four types, I recommend reading this article.

The idea behind ViewBindingis to create a binding class for every layout that holds the references of the views in the layout. This reduces boilerplate code in your component (dialog and adapter), and we can access views by creating an instance to that class. If you’re new to view binding, read the following article to learn about how to add the library and use it in high-level Android components such as activities and fragments.

Everything You Should Know About ViewBinding in Android

#kotlin #android #mobile

Android View Binding Components: Dialogs and Adapters
1.40 GEEK