View Binding is one of the best features which provides the views to bind with the activity which is ongoing. Replacing the findViewById() method, hence reducing the boilerplate code, generated the instances of the views of the current layout. And most important feature about the View Binding is it’s always null safe. In this article detailed it’s been provided in detail approach for the View Binding.
if interested in knowing more about View Binding check out my previous blog
- Implementation
Of couse open your ide of choice, for myself I am using Android Studio, and create a project with and empty activity. if you are unfamiliar with this process or is missing this activity template please follow this link.
- Enable ViewBinding
After the ide fininish initilizing everything we are going to open the module build.gradle
note: this project layout is supplied by the ide Android Studio
android { ... buildFeatures { viewBinding = true } }
- Modify XML
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/cl_layer" tools:context=".MainActivity" tools:ignore=""> <androidx.appcompat.widget.AppCompatTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_header" android:text="View Binding w/Kotlin" android:textSize="30dp" android:textAlignment="center" android:layout_marginStart="15dp" android:layout_marginTop="150dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.AppCompatEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_message" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="128dp" android:hint="Sir, your text here" app:layout_constraintEnd_toEndOf="@id/tv_header" app:layout_constraintStart_toStartOf="@id/tv_header" app:layout_constraintTop_toBottomOf="@id/tv_header" /> <androidx.appcompat.widget.AppCompatButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_submit" android:layout_marginTop="16dp" android:text="Submit" app:layout_constraintEnd_toEndOf="@id/et_message" app:layout_constraintTop_toBottomOf="@id/et_message" /> </androidx.constraintlayout.widget.ConstraintLayout>
UI Design
- Modify Activity class
package com.programmingninja.viewbinding import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.google.android.material.snackbar.Snackbar import com.programmingninja.viewbinding.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { //create instance ActivityMainBinding private lateinit var amb : ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //create the instance of ActivityMainBinding val binding = ActivityMainBinding.inflate(layoutInflater) //binding.root returns the root layout setContentView(binding.root) binding.btnSubmit.setOnClickListener { val msg = binding.etMessage.text.toString() if (!msg.isEmpty()) Snackbar.make(binding.clLayer, binding.etMessage.text.toString(), Snackbar.LENGTH_SHORT).show() else Snackbar.make(binding.clLayer, "Message is currently empty", Snackbar.LENGTH_SHORT).show() } } }
- Run that CODE
This new approach to finding views has an elegant interface, like Data Binding. Google designed it to avoid the performance issues of Data Binding and to provide compile-time safety.
See the completed code here: Fortress of Solitude