Welcome back.
And this video, we're going to look into view binding because we're going to keep on using view binding
from now on in the course.
So far, we've only used find view by ID and there is a better way around it if you want to get access
to items in your user interface.
And also, we're going to get rid of the action bar.
So let's start with the action bar problem slash situation because there's not really a problem.
It's just something that we need to fix.
And in the manifest, you could see that we are using the seven minutes workout theme.
And let's jump into it into our themes example where we can then change it to, well, from dark action
bar to actually no action bar.
OK.
And this will replace the action bar from, well, get rid of the action bar, basically.
So that's one thing.
Let's run the application once again.
You see how it's going to look now, before and now you see how it looks after.
So we don't have this entire action bar here, which allows us to go back or whatever and says the name
of the application and so forth.
We don't have that anymore and we don't need it.
All right.
And now let's look into view binding, OK, because we are going to use vue binding from now on and
in order to use vue binding, we need to set a couple of things up.
First of all, in the cradle file.
For our app.
We need to add to built settings.
He had an options build types and so forth inside of Android.
We add build features.
So where we need to use this curly brackets like so and say few binding.
True.
All right.
So we're basically saying we want to use view binding, we're activating it.
Once done, we sink the file, so synchronize it and now we can use view binding.
Now, inside of our main activity and the general inside of activities, we can now go ahead and use
view binding.
Therefore, how you go about it is you create a new variable.
And let me get rid of this as well here.
VAR binding colon and then you use the name of the activity.
So here activity main binding.
OK, so activity main is the XML file name and then you have the binding keyword.
So that's generally how it works.
This is automatically generated for each activity that you have.
This activity is called main activity.
So if you have an activity, for example, exercise activity, then it would be called activity exercise
binding.
All right.
So now we need to make it the knowledgeable and assign null to it.
We need to import it as well.
So let's import this class and you will find that it's now available here under data binding activity
mean binding.
And this is the name that I have for my project, but the directory for the project so that we can use
this binding.
So instead of setting the content view in here, we're going to set the content you off to preparing
our binding object.
So our binding object at this point is null as you see, and we can use activity mean binding and dot
inflate in order to now use their layout inflator to inflate the XML file.
So what it's going to do is it will basically prepare our binding object that we can now use it and
say binding dot root here.
So our set content view will now set it in a different way.
So this was how it was set before, right?
It's just said OK, use activity in XML, which is still fine, but this is the view binding approach.
So what it basically happens is it says use this XML file called activity main and inflate it with a
layout and Fletcher and store it inside of this binding object and now use the root of this binding
object, which is the main activities some L files root container, which is this constraint layout.
OK, so the constraint layout is the root of this entire binding object, but now what we can do is
instead of using the finite view by idea approach like we did here, we can go ahead and use view binding
instead.
So let's set that up real quick.
Therefore, we can just say binding that if a start and dart set on, click listener.
OK, so we don't need to create this variable anymore.
We don't need to create any variable for our user interface items.
We can directly access their IDs inside of this binding object because, as you know, it contains this
entire XML file, which also contains the individual ID so we can now access the ID directly, even
though we need to use question marks because the binding object is another bowl and the frame layout
is also infallible.
So at this point, we can go with it.
So we are well, you see, we don't have to do this stuff anymore, which we had to do quite a bit.
And also, if you wanted to do now, use this frame layout.
Outside of this method of this concrete method, we can still use view binding.
We don't have to set up this frame layout globally here at the top, for example, and then assign it
inside of the on create method and then we can only use it.
Now we can now use it throughout the entire file using this approach here, which is very cool.
Now, whenever you want to use view binding in your project, really make sure that you also use the
on destroy method where you.
Said binding back to null.
OK, so use the standard approach of the UN destroy, but then also make sure that binding is reset
to null.
Otherwise, you might run into problems.
OK, so this is just a clean way to unassigned a binding to avoid memory leak.
OK, so what is the purpose of view binding?
Well, the implementation is shorter and concise.
It is faster and more efficient during compile time.
It is a safer alternative because it ensures only the views interconnecting lay out are being referenced
because otherwise what could happen is, let's say we have an I.D. We call it false start.
But then in another XML file, we also have, I feel, start and then by accident, we are accessing
the wrong EFL's start and this would crash our application.
But now, with view binding, we are making sure that we are only specifically accessing the items.
The IDs of the file that fits to our exome L fits to our class.
So our main activity has an example assigned to it.
And only of that example, we can now access the IDs, which we now do because find view by ID works
throughout all files.
It doesn't just work for this particular file.
So if we had an file start in another XML file that has nothing to do with our activity in our application
would crash.
OK, so these are a couple of good reasons, I believe, and this is also the recommended approach.
So view binding how this whole thing is called as the approach that Google recommends for you to use
when it comes to applications that you're building from now on.
OK, so that was it for this video and the next one, we are going to look at how to add to exercise,
screen and use intents and customizing our toolbar.