Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🎨 UI – Views & Layouts  ·  Lesson 12

View Binding

The problem with findViewById

Kotlin
// ❌ findViewById – easy to use the wrong id or wrong type
val tvMessage = findViewById<TextView>(R.id.tvMessage)
val btnHello = findViewById<Button>(R.id.btnHelo)      // typo → crash only at runtime

findViewById

  • One line per view
  • Wrong id → crash at runtime
  • Wrong type cast → crash
  • Can return null

View Binding

  • All views ready in one object
  • Wrong id → compile error
  • Correct type automatically
  • Null-safe
Why every modern project uses View Binding

Step 1: Enable View Binding

Kotlin DSL – app/build.gradle.kts
// app/build.gradle.kts
android {
    // ...
    buildFeatures {
        viewBinding = true
    }
}
// then: File → Sync Project with Gradle Files

How names are generated

Layout fileBinding class
activity_main.xmlActivityMainBinding
fragment_notes.xmlFragmentNotesBinding
item_note.xmlItemNoteBinding
View idProperty
@+id/tvMessagebinding.tvMessage
@+id/btn_savebinding.btnSave (snake_case → camelCase)
(root view)binding.root
activity_main.xmlids: tvMessage, btnHelloGradle buildgeneratesActivityMainBinding.tvMessage .btnHello .root
One class per layout, one property per id

Step 2: Use it in an Activity

Kotlin – MainActivity.kt
package com.example.helloapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.helloapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var count = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)   // 1. inflate
        setContentView(binding.root)                           // 2. show its root view

        binding.btnHello.setOnClickListener {                  // 3. use ids as properties
            count++
            binding.tvMessage.text = "Namaste! You clicked $count times"
        }
    }
}

Using it in a Fragment

Kotlin – NotesFragment.kt
class NotesFragment : Fragment() {

    private var _binding: FragmentNotesBinding? = null
    private val binding get() = _binding!!          // valid only between onCreateView and onDestroyView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = FragmentNotesBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.tvTitle.text = "My notes"
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null                               // avoid memory leaks
    }
}
onCreateView_binding = inflate(...)onViewCreateduse binding.xxxonDestroyView_binding = null
The fragment binding pattern used in every screen of Student Diary

Using it in a RecyclerView adapter

Kotlin – NoteAdapter.kt
class NoteAdapter(private val items: List<String>) :
    RecyclerView.Adapter<NoteAdapter.VH>() {

    class VH(val binding: ItemNoteBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        VH(ItemNoteBinding.inflate(LayoutInflater.from(parent.context), parent, false))

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.binding.tvTitle.text = items[position]
    }

    override fun getItemCount() = items.size
}

Included layouts and ignoring a layout

XML + Kotlin
<!-- activity_main.xml -->
<include
    android:id="@+id/header"
    layout="@layout/layout_header" />

// Kotlin – the include becomes a nested binding
binding.header.tvHeaderTitle.text = "Dashboard"

<!-- to skip generating a binding class for a layout -->
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    tools:viewBindingIgnore="true" />

Common mistakes

  • Calling setContentView(R.layout.activity_main) AND inflating the binding → two copies; your listeners attach to the invisible one. Use setContentView(binding.root).
  • Forgetting Gradle sync → "Unresolved reference: databinding".
  • Using binding in a fragment after onDestroyView (e.g. in a late callback) → crash; check _binding != null.

Summary

  • Enable viewBinding = true; each layout gets an XxxBinding class.
  • Activity: inflate(layoutInflater) + setContentView(binding.root).
  • Fragment: _binding nullable, cleared in onDestroyView.
✅ Practice

Convert your BMI calculator from findViewById to View Binding. Count how many lines you removed.

Frequently Asked Questions

Is View Binding the same as Data Binding?
No. View Binding only generates references to views. Data Binding also lets you write expressions in XML. View Binding is simpler and faster to build, and is what most projects use.
Why do Fragments use _binding and binding?
A fragment can outlive its view. Setting _binding = null in onDestroyView releases the old views, preventing memory leaks; binding is a non-null getter used in between.
Does View Binding slow down my app?
No. The binding class is generated at build time and inflates the layout once — it is as fast as findViewById.
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicImageView, Drawables & Resources Your progress: 12 / 43 (28%) · Lesson 13 / 43 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free