Practice English Speaking&Listening with: Enabling View Binding on Android (Screencast #1)

Normal
(0)
Difficulty: 0

In this quick how-to I'm going to show you how to enable View binding in your android project

First you have to enable view binding by going to the module's build.gradle file and, inside the android block, typing view binding

{ enabled = true } . Now we click on "Sync now" to activate the new configuration and we're ready to go.

Let's now go to our XML layout, which is called activity_main.xml in this case, and we're going to give the textview an ID.

Now in the activity we're not going to use setContentView just yet so let's comment it out for now

Instead, we're going to get an instance of the binding class and we're going to let Android Studio search for it.

The name is generated from the name of the layout file so I can just search for "MainB" to find it.

Now we can call the static method `inflate()` on this class. And we pass the layoutInflater that we can access from the activity and

with that in place

We can simply access views as properties of the binding object and do things like changing the value of its text property or setting listeners

Finally we call setC ontentView, but, instead of passing a layout, we pass the root element of the binding

If we run the app now we get the new greeting.

:D

The Description of Enabling View Binding on Android (Screencast #1)