1
00:00:00,360 --> 00:00:00,960
Welcome back.

2
00:00:01,170 --> 00:00:05,520
And this video, we're going to look into view binding because we're going to keep on using view binding

3
00:00:05,520 --> 00:00:07,140
from now on in the course.

4
00:00:07,380 --> 00:00:14,520
So far, we've only used find view by ID and there is a better way around it if you want to get access

5
00:00:14,520 --> 00:00:16,290
to items in your user interface.

6
00:00:16,500 --> 00:00:19,050
And also, we're going to get rid of the action bar.

7
00:00:19,350 --> 00:00:24,360
So let's start with the action bar problem slash situation because there's not really a problem.

8
00:00:24,660 --> 00:00:26,250
It's just something that we need to fix.

9
00:00:26,550 --> 00:00:31,800
And in the manifest, you could see that we are using the seven minutes workout theme.

10
00:00:32,070 --> 00:00:41,430
And let's jump into it into our themes example where we can then change it to, well, from dark action

11
00:00:41,430 --> 00:00:44,580
bar to actually no action bar.

12
00:00:45,510 --> 00:00:45,690
OK.

13
00:00:45,780 --> 00:00:50,520
And this will replace the action bar from, well, get rid of the action bar, basically.

14
00:00:50,970 --> 00:00:52,200
So that's one thing.

15
00:00:52,530 --> 00:00:54,300
Let's run the application once again.

16
00:00:54,310 --> 00:00:58,440
You see how it's going to look now, before and now you see how it looks after.

17
00:00:58,680 --> 00:01:04,440
So we don't have this entire action bar here, which allows us to go back or whatever and says the name

18
00:01:04,440 --> 00:01:05,850
of the application and so forth.

19
00:01:05,850 --> 00:01:08,220
We don't have that anymore and we don't need it.

20
00:01:08,940 --> 00:01:09,350
All right.

21
00:01:09,400 --> 00:01:16,140
And now let's look into view binding, OK, because we are going to use vue binding from now on and

22
00:01:16,680 --> 00:01:21,480
in order to use vue binding, we need to set a couple of things up.

23
00:01:21,540 --> 00:01:24,030
First of all, in the cradle file.

24
00:01:25,600 --> 00:01:26,830
For our app.

25
00:01:28,260 --> 00:01:30,690
We need to add to built settings.

26
00:01:32,620 --> 00:01:36,790
He had an options build types and so forth inside of Android.

27
00:01:37,390 --> 00:01:40,000
We add build features.

28
00:01:42,550 --> 00:01:51,970
So where we need to use this curly brackets like so and say few binding.

29
00:01:53,100 --> 00:01:53,520
True.

30
00:01:54,150 --> 00:01:54,510
All right.

31
00:01:54,960 --> 00:01:58,580
So we're basically saying we want to use view binding, we're activating it.

32
00:01:58,590 --> 00:02:03,930
Once done, we sink the file, so synchronize it and now we can use view binding.

33
00:02:04,560 --> 00:02:11,400
Now, inside of our main activity and the general inside of activities, we can now go ahead and use

34
00:02:11,400 --> 00:02:11,940
view binding.

35
00:02:11,940 --> 00:02:15,540
Therefore, how you go about it is you create a new variable.

36
00:02:15,960 --> 00:02:17,730
And let me get rid of this as well here.

37
00:02:18,060 --> 00:02:24,750
VAR binding colon and then you use the name of the activity.

38
00:02:24,810 --> 00:02:27,690
So here activity main binding.

39
00:02:27,870 --> 00:02:32,250
OK, so activity main is the XML file name and then you have the binding keyword.

40
00:02:32,250 --> 00:02:33,570
So that's generally how it works.

41
00:02:33,580 --> 00:02:36,600
This is automatically generated for each activity that you have.

42
00:02:37,110 --> 00:02:38,910
This activity is called main activity.

43
00:02:39,150 --> 00:02:44,640
So if you have an activity, for example, exercise activity, then it would be called activity exercise

44
00:02:44,640 --> 00:02:45,270
binding.

45
00:02:45,690 --> 00:02:46,080
All right.

46
00:02:46,500 --> 00:02:50,250
So now we need to make it the knowledgeable and assign null to it.

47
00:02:50,280 --> 00:02:51,930
We need to import it as well.

48
00:02:52,410 --> 00:03:00,590
So let's import this class and you will find that it's now available here under data binding activity

49
00:03:00,610 --> 00:03:01,290
mean binding.

50
00:03:01,590 --> 00:03:07,800
And this is the name that I have for my project, but the directory for the project so that we can use

51
00:03:07,800 --> 00:03:08,490
this binding.

52
00:03:08,640 --> 00:03:14,730
So instead of setting the content view in here, we're going to set the content you off to preparing

53
00:03:14,730 --> 00:03:16,530
our binding object.

54
00:03:16,650 --> 00:03:24,420
So our binding object at this point is null as you see, and we can use activity mean binding and dot

55
00:03:24,630 --> 00:03:33,240
inflate in order to now use their layout inflator to inflate the XML file.

56
00:03:33,480 --> 00:03:39,870
So what it's going to do is it will basically prepare our binding object that we can now use it and

57
00:03:39,870 --> 00:03:42,900
say binding dot root here.

58
00:03:43,050 --> 00:03:47,230
So our set content view will now set it in a different way.

59
00:03:47,250 --> 00:03:49,210
So this was how it was set before, right?

60
00:03:49,230 --> 00:03:55,710
It's just said OK, use activity in XML, which is still fine, but this is the view binding approach.

61
00:03:56,130 --> 00:04:02,910
So what it basically happens is it says use this XML file called activity main and inflate it with a

62
00:04:02,910 --> 00:04:09,600
layout and Fletcher and store it inside of this binding object and now use the root of this binding

63
00:04:09,600 --> 00:04:18,000
object, which is the main activities some L files root container, which is this constraint layout.

64
00:04:19,200 --> 00:04:25,680
OK, so the constraint layout is the root of this entire binding object, but now what we can do is

65
00:04:25,680 --> 00:04:32,100
instead of using the finite view by idea approach like we did here, we can go ahead and use view binding

66
00:04:32,100 --> 00:04:32,450
instead.

67
00:04:32,460 --> 00:04:34,050
So let's set that up real quick.

68
00:04:34,620 --> 00:04:41,990
Therefore, we can just say binding that if a start and dart set on, click listener.

69
00:04:42,870 --> 00:04:45,750
OK, so we don't need to create this variable anymore.

70
00:04:45,760 --> 00:04:49,590
We don't need to create any variable for our user interface items.

71
00:04:49,950 --> 00:04:56,820
We can directly access their IDs inside of this binding object because, as you know, it contains this

72
00:04:56,820 --> 00:05:04,980
entire XML file, which also contains the individual ID so we can now access the ID directly, even

73
00:05:04,980 --> 00:05:09,900
though we need to use question marks because the binding object is another bowl and the frame layout

74
00:05:09,900 --> 00:05:10,920
is also infallible.

75
00:05:11,550 --> 00:05:13,830
So at this point, we can go with it.

76
00:05:14,280 --> 00:05:19,020
So we are well, you see, we don't have to do this stuff anymore, which we had to do quite a bit.

77
00:05:19,020 --> 00:05:22,770
And also, if you wanted to do now, use this frame layout.

78
00:05:22,770 --> 00:05:27,590
Outside of this method of this concrete method, we can still use view binding.

79
00:05:27,600 --> 00:05:35,640
We don't have to set up this frame layout globally here at the top, for example, and then assign it

80
00:05:35,640 --> 00:05:38,550
inside of the on create method and then we can only use it.

81
00:05:38,910 --> 00:05:43,410
Now we can now use it throughout the entire file using this approach here, which is very cool.

82
00:05:45,750 --> 00:05:53,020
Now, whenever you want to use view binding in your project, really make sure that you also use the

83
00:05:53,040 --> 00:05:55,290
on destroy method where you.

84
00:05:57,160 --> 00:05:58,390
Said binding back to null.

85
00:05:58,930 --> 00:06:05,230
OK, so use the standard approach of the UN destroy, but then also make sure that binding is reset

86
00:06:05,230 --> 00:06:05,620
to null.

87
00:06:05,650 --> 00:06:08,410
Otherwise, you might run into problems.

88
00:06:08,620 --> 00:06:13,960
OK, so this is just a clean way to unassigned a binding to avoid memory leak.

89
00:06:15,470 --> 00:06:18,530
OK, so what is the purpose of view binding?

90
00:06:18,860 --> 00:06:22,340
Well, the implementation is shorter and concise.

91
00:06:22,370 --> 00:06:24,950
It is faster and more efficient during compile time.

92
00:06:25,340 --> 00:06:32,450
It is a safer alternative because it ensures only the views interconnecting lay out are being referenced

93
00:06:32,930 --> 00:06:37,910
because otherwise what could happen is, let's say we have an I.D. We call it false start.

94
00:06:37,910 --> 00:06:42,710
But then in another XML file, we also have, I feel, start and then by accident, we are accessing

95
00:06:42,710 --> 00:06:45,500
the wrong EFL's start and this would crash our application.

96
00:06:45,980 --> 00:06:53,870
But now, with view binding, we are making sure that we are only specifically accessing the items.

97
00:06:53,870 --> 00:07:01,250
The IDs of the file that fits to our exome L fits to our class.

98
00:07:01,310 --> 00:07:04,250
So our main activity has an example assigned to it.

99
00:07:04,490 --> 00:07:11,300
And only of that example, we can now access the IDs, which we now do because find view by ID works

100
00:07:11,300 --> 00:07:12,650
throughout all files.

101
00:07:12,890 --> 00:07:15,200
It doesn't just work for this particular file.

102
00:07:15,560 --> 00:07:21,710
So if we had an file start in another XML file that has nothing to do with our activity in our application

103
00:07:21,710 --> 00:07:22,190
would crash.

104
00:07:23,090 --> 00:07:28,400
OK, so these are a couple of good reasons, I believe, and this is also the recommended approach.

105
00:07:28,400 --> 00:07:34,850
So view binding how this whole thing is called as the approach that Google recommends for you to use

106
00:07:34,850 --> 00:07:38,510
when it comes to applications that you're building from now on.

107
00:07:39,680 --> 00:07:45,780
OK, so that was it for this video and the next one, we are going to look at how to add to exercise,

108
00:07:45,780 --> 00:07:49,310
screen and use intents and customizing our toolbar.

