View Binding: The New Sheriff in Town — Advantages Over Kotlin Synthetics and Databinding

Throwgether
TakoDigital
Published in
4 min readDec 15, 2023

As Android developers, we’ve all grappled with binding views. We’ve fought findViewById tyranny, embraced Kotlin Synthetics’ magic, and explored the data-driven power of Databinding. But now, a new sheriff has arrived: View Binding. And let me tell you, it’s got some serious advantages over the old guard.

View binding:

Use view binding in activities
To set up an instance of the binding class for use with an activity, perform the following steps in the activity’s onCreate() method:

Kotlin Synthetics:
This approach utilized the kotlin-android-extensions plugin to magically access views by their ID directly within your Kotlin code. Imagine having an activity layout with a TextView with ID text_view. In Kotlin Synthetics, you could simply access it directly like this:

text_view.text = “hallo world!”

Against the Frayed Rope: View Binding vs. kotlinx.android.synthetic

Remember the days of endless findViewById calls? View Binding throws that rope away and replaces it with a shiny, type-safe bridge. Here's how it wins:

  • Null Safety: No more null checks! View Binding generates a class with typed references to your views, eliminating null pointer exceptions at compile time.
  • Faster Development: Say goodbye to boilerplate code. View Binding automatically generates binding classes, saving you precious typing time.
  • Clean Code: No more synthetic properties cluttering your code. Your views are explicitly declared, making your code more readable and maintainable.
  • Clearer Errors: Compile-time errors pinpoint issues with your views, eliminating the frustration of runtime crashes.

Databinding:
Databinding took a different approach, introducing data-driven view binding. You defined data bindings in your layout XML, linking views to variables holding data. Then, the Android framework generated boilerplate code that updated the views whenever the data changed.

Facing the Data Dragon: View Binding vs. Databinding

Databinding offers data-driven magic, but View Binding brings simplicity and speed. Here’s where it shines:

  • Faster Build Times: View Binding skips the annotation processing dance, resulting in significantly faster build times. No more waiting for Databinding to weave its spell.
  • Smaller APK Size: No need for a dedicated binding library, making your app leaner and meaner.
  • Easier for Java Developers: Kotlin Synthetics and Databinding were Kotlin-first citizens, leaving Java devs in the dust. View Binding welcomes Java devs with open arms.
  • Focus on UI Logic: View Binding keeps things simple, focusing on binding views and not data manipulation. This makes it ideal for simpler UIs and projects where data binding is overkill.

But wait, there’s more! View Binding plays nicely with other tools:

  • MVVM and MVI Harmony: View Binding seamlessly integrates with your favorite architectures, keeping your code clean and separation of concerns intact.
  • Testing Made Easy: With typed views and compile-time checks, testing becomes a breeze. No more mocking or fragile data binding logic.

So, is View Binding the ultimate solution? Not quite. Databinding still reigns supreme for complex UIs and data-driven scenarios. But for simpler projects, View Binding offers a compelling combination of speed, clarity, and ease of use.

Embrace the new era, fellow developers! Give View Binding a try, and you might just find yourself saying goodbye to the old ways of binding views.

Bonus Tip: Want to learn more? Check out the official documentation and community resources. The Android community is always eager to share knowledge and help you on your View Binding journey.

I hope this helps you write a compelling article about View Binding. Feel free to adapt and expand on it to fit your specific audience and style!

Ref :
https://developer.android.com/topic/libraries/view-binding
https://developer.android.com/topic/libraries/view-binding/migration
Compose by:
https://bard.google.com/
https://chat.openai.com/

--

--

Throwgether
TakoDigital

เมื่อเด็กเกาะเบาะต้องลงมือ Dev เอง