Android View Binding

View Binding

View Binding is the way to manipulate UI easily

findViewById is one of annoying and fat codes in Activity

There are a lot of library to support this.

In this entry, there are 2 ways to show without external library (Only Android provided)

  • Kotlin Android Extension
  • View Binding Android 3.6 above

The benefit is not to wrap <layout></layout> like other library and null safe.

And bye bye findViewId

Kotlin Android Extension

These days, this plugin seems to be default.

build.gradle

apply plugin: 'kotlin-android-extensions'


android {
   viewBinding {
            enabled = true
   }
}

That’s it. Let’s go layout xml code

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
       <Button
                android:id="@+id/sample_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Binding!"/>

   </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

We don’t need to wrap <layout></layout> different from library based one.

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        sample_button.setOnClickListener {
            // Do something
        }
    }
}

sample_button is just id from layout file. Without definition, we can use instance.

View Binding Android 3.6 above

This sample is basically from Android view-binding page

activity_binding.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <ImageView android:cropToPadding="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

After this, we can use ActivityBindingBinding class (auto generated) for Binding.

This name is from activity_binding.xml -> ActivityBinding + Binding = ActivityBindingBinding

The class name is from XML file name

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityBindingBinding

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

        binding.button.setOnClickListener({
            Log.d("Binding", "Click") // Operation of viewmodel
        })
    }
}

We can access Button using id name. And we don’t need to call findViewById.

Android
スポンサーリンク
Professional Programmer2

コメント