UI Handling in Android Activities: XML Layouts, setContentView(), and View Binding

How to Handle UI Views Inside an Activity using setContentView(), and View Binding

UI Handling in Android Activities: XML Layouts, setContentView(), and View Binding

Introduction

When building Android applications, managing the User Interface (UI) inside an Activity is a fundamental skill. Android provides multiple ways to define and manipulate UI components, including XML layouts, findViewById(), and View Binding.

In this article, we'll cover:

  • Understanding setContentView() and XML layouts.

  • Using findViewById() vs. View Binding for UI interactions.

  • The pros and cons of each approach.

What is UI Handling in an Activity?

An Activity serves as the entry point for user interactions in an Android app. To display UI elements, an Activity needs an XML layout file, which is set using setContentView(). After setting the layout, the UI elements can be referenced in Kotlin or Java using findViewById() or View Binding.

Understanding setContentView() and XML Layouts

What is setContentView()?

setContentView() is a method in the Activity class that defines which XML layout should be used for the UI.

Example: XML Layout (activity_main.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!" />
</LinearLayout>

Example: Setting the Layout in an Activity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

This ensures that activity_main.xml is displayed when the Activity starts.


Using findViewById() to Access UI Elements

Before View Binding, the common way to reference UI elements in an Activity was by using findViewById().

Example: Using findViewById()

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView: TextView = findViewById(R.id.textView)
        textView.text = "Updated Text!"
    }
}

Issues with findViewById()

  • Not Type-Safe – Requires explicit casting in Java.

  • More Boilerplate Code – Each UI element needs a separate findViewById() call.

  • Prone to Runtime Errors – Can cause NullPointerException if the ID is incorrect or missing.


What is View Binding?

View Binding is a type-safe and efficient way to interact with UI elements in Android. It generates a binding class for each XML layout, allowing direct access to views without using findViewById().

Enabling View Binding

To enable View Binding, add the following inside build.gradle (Module: app):

android {
    buildFeatures {
        viewBinding = true
    }
}

Example: Using View Binding in an Activity

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.textView.text = "Hello, View Binding!"
    }
}

Advantages of View Binding

  • Type-Safe – No need for explicit casting.

  • Less Boilerplate Code – Eliminates findViewById().

  • Better Performance – Faster UI access.


Comparing findViewById() vs. View Binding

FeaturefindViewById()View Binding
Requires Casting✅ Yes❌ No
Type Safety❌ No✅ Yes
Boilerplate Code❌ More✅ Less
Performance❌ Slower✅ Faster

Recommendation: Always use View Binding for cleaner and safer UI handling.

Conclusion

  1. Use View Binding for a modern, efficient, and safer approach to UI interactions.

  2. Avoid findViewById() unless working on older projects.

  3. Always call setContentView() before accessing views in an Activity.