Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🎨 UI – Views और Layouts  ·  Lesson 12

View Binding

findViewById की problem

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

  • हर view के लिए एक line
  • गलत id → runtime पर crash
  • गलत type cast → crash
  • null लौटा सकता है

View Binding

  • सारे views एक object में तैयार
  • गलत id → compile error
  • सही type अपने आप
  • Null-safe
हर modern project View Binding क्यों use करता है

Step 1: View Binding enable करें

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

Names कैसे बनते हैं

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 buildबनाता हैActivityMainBinding.tvMessage .btnHello .root
हर layout की एक class, हर id की एक property

Step 2: Activity में use

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"
        }
    }
}

Fragment में use

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(...)onViewCreatedbinding.xxx use करेंonDestroyView_binding = null
Student Diary की हर screen में यही fragment binding pattern

RecyclerView adapter में use

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 और layout ignore करना

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" />

आम गलतियाँ

  • setContentView(R.layout.activity_main) भी करना और binding भी inflate करना → दो copies; listeners अदृश्य copy पर लगते हैं। setContentView(binding.root) use करें।
  • Gradle sync भूलना → "Unresolved reference: databinding"।
  • Fragment में onDestroyView के बाद (जैसे देर से आए callback में) binding use करना → crash; _binding != null check करें।

सारांश

  • viewBinding = true enable करें; हर layout को XxxBinding class मिलती है।
  • Activity: inflate(layoutInflater) + setContentView(binding.root)।
  • Fragment: nullable _binding, onDestroyView में clear।
✅ Practice

अपने BMI calculator को findViewById से View Binding में बदलिए। गिनिए कितनी lines कम हुईं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या View Binding और Data Binding एक ही हैं?
नहीं। View Binding सिर्फ़ views के references बनाता है। Data Binding XML में expressions भी लिखने देता है। View Binding आसान और build में तेज़ है, और ज़्यादातर projects यही use करते हैं।
Fragments में _binding और binding क्यों?
Fragment अपने view से ज़्यादा देर ज़िंदा रह सकता है। onDestroyView में _binding = null करने से पुराने views छूट जाते हैं और memory leak नहीं होता; बीच में binding non-null getter है।
क्या View Binding app को धीमा करता है?
नहीं। Binding class build time पर बनती है और layout एक बार inflate करती है — findViewById जितनी ही तेज़।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

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

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

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

अगला topicImageView, Drawables और Resources आपकी प्रगति: 12 / 43 (28%) · Lesson 13 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें