Skip to content
tezvyn:

ViewBinding: Say Goodbye to findViewById

Source: developer.android.comMediumHow cards are made

ViewBinding: Say Goodbye to findViewById

ViewBinding generates a type-safe class from your XML layout, giving you direct, non-null access to views with IDs. It's a compile-time safe replacement for findViewById used in Activities and Fragments. If a view is missing, you forgot to give it an ID.

Why it exists

Before ViewBinding, developers used findViewById to get references to UI elements. This was slow and unsafe: it required explicit type casting, was prone to NullPointerException if an ID was wrong, and wasn't checked at compile time, leading to runtime crashes.

The mental model

Think of ViewBinding as an automatic code generator that acts as a bridge between your XML layout and your Kotlin/Java code. For every layout file, it creates a corresponding "binding" class that holds direct, type-safe properties for every view that has an ID. You ask for binding.userNameTextView instead of findViewById(R.id.user_name_text_view). This eliminates lookup boilerplate and moves ID errors from runtime crashes to compile-time failures.

How it works

First, you enable it in your module's build.gradle file by setting buildFeatures { viewBinding = true }. After a build, Android Studio generates a binding class for each XML layout. The class name is derived from the layout file name (e.g., activity_main.xml becomes ActivityMainBinding). This class contains a property for each view with an android:id. For example, a TextView with android:id="@+id/name" becomes a TextView property named name on the binding object. It also provides a root property that refers to the layout's top-level view.

When to use it

Use ViewBinding in almost all cases where you need to reference views from your code in Activities and Fragments. It is the recommended, modern approach for interacting with views defined in XML. It provides a clean, safe, and efficient alternative to findViewById and is less complex than its more powerful sibling, DataBinding.

When not to use it

ViewBinding is specifically for layouts you inflate and manage yourself. If you have a layout file that is never directly used by your app code, you can tell the generator to ignore it by adding tools:viewBindingIgnore="true" to the root element to save compile time. It's also overkill for a layout with no views that need programmatic access.

One canonical example

To use ViewBinding in an Activity, you first declare a binding variable. Then, in onCreate, you inflate the layout and set the content view in three lines:

binding = ResultProfileBinding.inflate(layoutInflater)

val view = binding.root

setContentView(view)

After this setup, you can access any view with an ID directly, like binding.name.text = "Jane Doe" or binding.button.setOnClickListener { ... }.

Interview question

What is the primary advantage of using ViewBinding over findViewById in Android development?

  • a.It eliminates the need for defining IDs in XML layout files.
  • b.It prevents runtime crashes from missing view IDs and eliminates explicit type casting.Correct
  • c.It enables direct two-way data synchronization between UI and data models.
  • d.It automatically handles complex UI animations and transitions.
Why?

ViewBinding's core benefit is providing compile-time safety, preventing runtime NullPointerExceptions from incorrect view IDs, and removing the need for explicit type casting. Option C describes DataBinding, a different technology.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles