This course focuses on lifecycles and intent in Android and then moves on to look at services, receivers, and Android app binding.
Intended Audience
This course is intended for anyone who wants to learn how to start building their own apps on Android.
Prerequisites
To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.
Resources
Course GitHub repo: https://github.com/OakAcademy/android-app-development-with-kotlin/tree/main/Section%207%20-%20Intent%20and%20Lifecycle/Intent
Hello my friends. In this video, we're going to talk about Android View Binding. I know it's the topic that's on everybody's minds these days. But I want to tell you that while you're developing an android application, just linearly if you will. The first thing you do, is create a design for the application then define components that we use in the design area in the colon class. And then, you write the necessary code. So, we used, find view by ID, to define the components in the design area in Colon, right?
This method allows us to match the variables we created in the cotton class with a component in the design area. Then we wrote the necessary code using the variables corresponding to each component. Right? Of course, defining the components on the common side was a lot of work, sometimes even tiring for a lot of us. But, it also causes us to generate a lot of extra code.
Don't you think? For example, imagine that there are 20 components in the design area and you need to define these 20 components in the cotton and class. It means that you've got to create 20 variables and type the findViewById() method 20 times. Well, how boring is that? That's not why you're doing this. Right? Well, this is where View Binding comes into play. So, it's actually possible to get rid of all of this cruelty. Thanks to a line of code that we can just add right into the build that great old section of our project. See how it just gets better and better, right? After you get all this sort of fundamental stuff out of the way. Anyway, after making our project ready for use in view binding, it's possible to directly access all the components in the design area provided that they use their IDs. So, what are you doing sitting around listening to this? Let's get into android studio and learn how to do view binding with some practice. Alright, so I'm creating a new project for this lesson name of the project, is view binding and we're going to make a very simple design here and there's just going to be one edit text, one Button, one text view in the design area. And first of all, I want to change the constraint layout to linear layout on the xml side and I'll write linear layout instead of constraint layout. Also, the orientation of this linear layout is going to be vertical. Okay, so now we can make our application design. So, first I want to add an edit text to the design area and now, let's add a button. Finally, let's add a text view and I'm going to change the layout width of these components to wrap content.
Also, I just want to delete the text of the edit text component and I want to add a hint so we can write your name in the hints section. That's not a hint. That's a hammer to that. But you know what I mean? It's a hint. Let's just make the text size of the text view component 24 SP. Also, I want to make the gravity property of this linear layout, center_horizontal. layout_marginTopProperty of the edit text can be 100 DP. layout_marginTopProperty of the Button in text view can be 20 DP. Okay, so the user is going to enter a name in the edit text component and when they click the button then name in the edit text will be printed in the text view component. Okay, pretty simple. But, you'll see what we're going to do with it. So now, let's give an id to these components. But before you start putting in ids, I want to mention that you should define the id of the components, to access the components with view binding.
Otherwise, you won't be able to access the components that don't have an id, that you have added to the design area. Right? So, to show this too I'm deleting the id of the, edit text component. So, for now I'll just give an id to the button and the text view, you follow? So, the id the button can be buttonOk. Okay, the id of the text view component can be textViewResult. So, my friends, there's the design very simple like I said, but now, we can define view binding to our project. So, first of all from the project directory, just open up the build.gradle module app. And it's the file here in the gradle script section. So, I want you to notice that there's a field called android. How convenient. So many, many, many details about our project can be found in here.
Now, we will add a new field called buildFeatures to this android section and adding curly braces to scope it out of it, then I write, viewBinding = true, inside these curly braces. And that way I'll get to use view binding in our project and well, you have just got to add the field. That's all. So now, I want to click 'Sync now' in the upper right corner and start the synchronization process and when the synchronization is complete, we can go and open MainActivity. First, we're going to create a variable. This variable we will create will be from the activity Main binding class. After adding the view binding to our project. The class is automatically created with the name of the activity that you're using. Right? That's why you want to use consistent names. Anyway, or just name it the way I name or do what I tell you. Never mind. For example, let's say you have a second activity in your project. Its name is game activity.
So, in this case the binding class that you will use in this activity will be activity Game binding. A separate binding is created for each activity. That's all I wanted to say. Alright, so now let's create our variable. So, I will write, lateinit. The name of the variable can be mainBinding : ActivityMainBinding. Now, in the onCreate method, just above the setContentView line here, I write, MainBinding = ActivityMainBinding.inflate. You got to remember the inflate method? Right? We use the inflate method in our recycler view and the grid view lessons. So, this method is well, it's a great method and you use it when you want to transfer a design that you have, to the code side. So, I'm writing (LayoutInflater) as the parameter of this method and then after that, I'm creating a new variable on the bottom line and I'll write val_view = mainBinding.root Cool. Finally I'll delete the parameter of the set content view and write the view variable here.
Alright so, that way we have defined view binding to our project and matched the design that we created for the main activity with the main binding object. Now, the main binding object represents the activity main design. Right? So, using this variable we can directly access the components in the design area. You see where we're going with this. So, we no longer need to use the findViewById method. That's a relief. So now, we've got to write the code of our application. So, first let's add a click listener to the button and all write, MainBinding. after the dot as you can see, we can directly access the button and the text view components on the design area from here. I'll just choose buttonOk.setOnClickListener. Now, let's take all that data that the user is written in the edit text component and print it in the text view component. So, I'm writing, mainBinding.textViewResult.text= and then after the equal sign. Alright. MainBinding. but if you notice, we cannot access the edit text component. Why is that? Because we didn't specify the id of the edit text component. If you do want to access the components in the design area with the view binding, you've got to define the ids of the components first. Right? So, now let's go to the design area. See I planted that on purpose and determine the idea of the edit text component. It is a very simple fix. So, here I'll just write the editTextName as the ID. Right?
So, now let's move on to the main activity and continue where we left off. MainBinding.editTextName.text.toString(). Now, let's run it and test the application. Okay, so the application opens let's write in a name and let's say the name will be David, click the 'Button' and there you go. Applications running successfully. So, there you go, my friend. That is how view binding is used. So, by using view binding, do you see that you can directly access components in the design area just by specifying their ids. What you do need to do first is define the view binding features in the build.gradle file and then, create a variable from the binding class in which activity you want to use it. Alright. And access to components using this variable. If you do have more than one activity in your application, of course, you will. You're going to need to create a variable for each activity and then define it just like we did. Alright. Just got to remember to set that up and it's all, it's all yours. Alright, so we're going to take a short break here. I'll see you in the next one.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.