Textview Binding
Start course
3h 39m

This course begins by downloading Android studio - make sure you watch the appropriate video depending on whether you're on Mac or Windows. Then, we'll take a tour off the Android Studio interface and see how apps are put together. You'll learn about text views, buttons, and images to build a user interface for our app, and we'll also write some code to make our apps interactive.

Then we'll move onto a practical project in which we make a temperature converter app, which converts temperatures from Fahrenheit to Celsius. You'll be able to follow, building the app, and then running it on your system.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.


Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.


Hello and welcome back. In the last video, we were able to refer to a view element from our layout in our code by using this findViewById. Now, this is no longer the preferred way of working with view elements that we add in our layout. And there are a couple of reasons for this, but they're a bit too advanced and complex to get into at this time. But in general, the solution is using what's called a View Binding. Let's pull it up in a browser window. Here we go. 

So, go to a new browser window and type in android kotlin view binding. And the first result should be the one from So, basically, view binding is a feature that allows you to more easily write code that interacts directly with your views. And once a binding is enabled, it generates a binding class for each layout file present in that module. And remember, the layout file that we're working with is the activity main layout file. So, once we enable a binding, it will generate a class for this layout file for us. And once we have that object, that object will contain direct references to all of the views that have an ID in this layout. So, this view, where we're working with the name, then this button, which has the ID of button. 

So, once we have the binding set up, we can reference any of these view objects directly using their ID. So, let's go ahead and set it up. I'm going to go back to the instructions. Here we go. And feel free to read through this if you have some time, it'll give some more information on it. So, first we're going to enable this in our build.gradle file. So, if you pull up your code, I'm going to open the project's tab, and you see under Gradle Scripts, expand it, and this build.gradle, the module right here, not the project but the module file, open this. And here we have to add some code. We're going to say, buildFeatures and then open and close curly braces, and within it, we're going to say, viewBinding. 

Again, camel case here for all of this. viewBinding and then true, alright. And once you make this update, you have to sync this with your project. Sync this gradle file with your project. So, let's go ahead and do that. Sync now. Okay, great. That's done. It's synced. The next step if you look at the instructions. I'm going to scroll down. Here is usage instructions. I want to scroll down. Here we go. Kotlin code is provided by default. For those of you interested in looking at the Java code, that's also provided here, but we're using Kotlin. 

So, right here, the first step is to create this binding variable so that it's available for us. It's a matter of basically copying this line and customizing it. So, back here, I'm going to exit out of this and go to my main activity kotlin file. And on top here, above the function onCreate. Now, I can do something inside this function, but then my binding won't be available for my other functions and I want it to be available for any other functions that I create. That's why that first line is important. So, I'll say private lateinit var binding:. And the name here is going to be activity main binding, alright? So, whatever your layout file is, for us it's Activity_main, so ActivityMainBinding. 

So, it shows up, ActivityMainBinding. And as you saw, this also imported this package right here, data binding ActivityMain binding. I'm going to compress this. And now I have this variable binding that I can use, but I have to do some more setup. So, let's go ahead and check it out. That was the first line right there. And then I'm sending binding equals whatever the name was. So, for us it was activityMainBinding.inflate layoutInflater. Then it's setting view to binding.route and setContentView to view. So, these three lines, I'm going to work with in my code. So, I'm going to copy the first line back to my code over here. Alright. So, right after this super.onCreate, I'm going to say this binding, instead of ResultProfileBinding, mine is ActivityMainBinding. 

So, I'm going to change this to ActivityMainBinding.inflate (layoutInflater), all right. And now that I have this binding object, setContentView is no longer resources(R.layout.Activity_main), but setContentView is going to be binding.root. Again, this is done in two steps over here, but I'm directly going to take this binding.root and enter that in as an argument for setContentView. So, right here, setContentView binding.root. Okay. So, that's it for the setup. There are no additional steps if you look here. Now, it walks you through some example code and you can take a look at this if you want, but I'll directly go back to my code and show you how we can modify this. I don't need to use any of this anymore. What I can do is directly say something like this, binding. and then the ID of any of my views. So, right here you see button and edit name field, those are the two fields that I'm working with they show up. 

So, if I wanted to get edit name field, you can just grab that and I can reference it this way. So, if I wanted to text for that field, which I did in this line, in the next line, I can simply say .text like that. All right. And then I don't need this next line either. So, instead of using a variable here, what I can do is grab this 'Cmd+X', and directly put that in here. So, binding.editNameField.text.toString, right? So, let's see if this works. I'm going to run my app. Let's pull up the emulator. There we go. My app's starting. It succeeded. Okay, I'm going to pull up my logs, just so I have it, all right? And then here's my emulator. Lets type in my name, and then click the button. There you go. Enter name: Hussein, and then button pressed. All right. 

Let's try another one, Mashrur, button. Perfect. Enter name and button pressed. All right. So, still working great. So, now that you know this method as well, you can use both findViewById if you like or this one, I leave that up to you. I'll use findViewById for one or two more videos and then I'll switch to binding for most. But for practice, you can try using binding for the next video where I use findViewById so you can get some more practice, all right? So, one more thing, in the past, there used to be another package called Android Synthetic or Synthetic Android, and this wasn't provided by Google, but was developed by JetBrains, the company that created the Kotlin programming language. And using that package, you could actually do something like this even without using the binding variable, you could directly reference a view by its ID. 

So, editNameField, you wouldn't even need to say, binding.editNameField, you could directly reference the field by its ID from here. But this has been discontinued and no longer provided by default, so I'm not going to cover it, but you can still enable this and it involves adding another plugin called Kotlin android extensions. So, if I pull up my build.gradle, you'd have to add the Kotlin android extensions plugin over here. But again, since it's not the preferred way and discontinued by default, I'm not going to cover it here, but I just wanted to show you that that's another way of working with view objects that used to be available. All right, so that's all for this video. I hope you enjoyed it, and I'll see you in the next one.


About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics