1
00:00:00,120 --> 00:00:00,840
Welcome back.

2
00:00:00,930 --> 00:00:06,480
And this video, we're going to look at dialogues and dialogues are going to be required for further

3
00:00:06,480 --> 00:00:09,980
projects that we're going to build and we're going to use them every now and then.

4
00:00:09,990 --> 00:00:15,060
And that's why it makes sense to have a deeper look at them because we're also going to require them

5
00:00:15,240 --> 00:00:17,670
for this project that we're currently working on.

6
00:00:17,910 --> 00:00:19,680
So let's get right into it.

7
00:00:20,010 --> 00:00:24,720
So what I have here is a project, and I'm not going to build everything here in this video.

8
00:00:24,720 --> 00:00:29,910
I'm just going to show you two different types of dialogues that there are and how you can build them

9
00:00:29,910 --> 00:00:30,450
in code.

10
00:00:30,820 --> 00:00:33,240
And yeah, let's just get started.

11
00:00:33,630 --> 00:00:38,220
So first of all, we have this little image button, and once I click on it, you see, I get this text

12
00:00:38,220 --> 00:00:38,880
here at the bottom.

13
00:00:39,210 --> 00:00:45,330
So this text is not as we have seen before, a toast, but this is a snack bar.

14
00:00:45,660 --> 00:00:47,700
So I can click multiple times on it.

15
00:00:47,700 --> 00:00:53,280
And as you can see, it's constantly replacing the old one, so it's getting rid of the old snack bar

16
00:00:53,280 --> 00:00:54,540
and then it's adding the new one.

17
00:00:54,540 --> 00:00:56,670
And also you can swipe it away.

18
00:00:57,060 --> 00:00:59,100
So here again, swipe it away.

19
00:00:59,520 --> 00:01:01,320
OK, so this is a snack bar.

20
00:01:01,680 --> 00:01:04,769
How does a snack bar differ from a.

21
00:01:06,250 --> 00:01:08,110
Toast, you might wonder.

22
00:01:08,290 --> 00:01:09,100
All right, let's look at it.

23
00:01:10,950 --> 00:01:13,020
8OZ was available right from the start.

24
00:01:13,350 --> 00:01:16,410
Snack bars were only added than the API level 23.

25
00:01:16,860 --> 00:01:22,770
The activity is not required to be shown in Android when you want to run it also.

26
00:01:23,160 --> 00:01:24,510
What can happen is you run it.

27
00:01:24,510 --> 00:01:27,630
Most activities closed and toast still persists.

28
00:01:28,050 --> 00:01:28,440
All right.

29
00:01:28,800 --> 00:01:31,050
In snack bars, that's not the case.

30
00:01:31,060 --> 00:01:35,460
So the snack bar will only be visible in the activity of the application.

31
00:01:36,090 --> 00:01:40,170
It can't be, or the host can perform an action based on user input.

32
00:01:40,650 --> 00:01:43,020
In the snack bar, you can set something up.

33
00:01:43,020 --> 00:01:46,470
So if the user clicks on the snack bar, then you can run some additional code.

34
00:01:47,960 --> 00:01:55,220
The toss cannot be dismissed by swiping well, the snow part can and toes cannot handle user input like

35
00:01:55,220 --> 00:02:01,490
a swipe, etc. snack bar can, and the toast is good for showing and for messages to user.

36
00:02:01,490 --> 00:02:07,880
And the snack bar is good for showing warning info, type messages to users or that need attention so

37
00:02:07,880 --> 00:02:14,870
you can learn more about it here on this Stack Overflow post, you can just Google Android snack bar

38
00:02:14,870 --> 00:02:15,650
versus toast.

39
00:02:15,980 --> 00:02:18,620
You can find some more information about that here.

40
00:02:19,430 --> 00:02:21,680
All right, now, let's get back to our code.

41
00:02:22,310 --> 00:02:25,670
We have some other dialog types that are interesting.

42
00:02:25,880 --> 00:02:30,920
Even though the snack bar is not really a dialog type, some sort of output that is visible to the user.

43
00:02:31,250 --> 00:02:34,220
All right, now, let's look at how we can create such a snack bar.

44
00:02:35,030 --> 00:02:39,170
Well, we have this image a button set on click snack bar.

45
00:02:39,680 --> 00:02:45,490
So here, very simple one line of code to create a snack bar, snack bar to make.

46
00:02:45,500 --> 00:02:48,080
And then you have to pass it in to view.

47
00:02:49,580 --> 00:02:56,420
You have to write a little text that you want to display, then the length of the snack bar and then

48
00:02:56,430 --> 00:02:57,080
Dutch shell.

49
00:02:57,350 --> 00:03:01,370
So very similar to what we have seen when it comes to toasts.

50
00:03:02,680 --> 00:03:04,140
All right, now, let's look at the next one.

51
00:03:04,470 --> 00:03:10,010
We have alert dialogues here, so let's click on one and you can see alerts and there's a little icon.

52
00:03:10,020 --> 00:03:16,830
Does this alert dialogue, which is used to show alerts on your app and you can cancel say no or yes?

53
00:03:17,430 --> 00:03:20,430
OK, now how do we create such an alert?

54
00:03:20,490 --> 00:03:22,560
Well, first of all, let's click on to cancel.

55
00:03:22,920 --> 00:03:27,210
Can see Click Cancel Operation Cancel once again the alert dialog.

56
00:03:27,510 --> 00:03:28,110
Let's click on.

57
00:03:28,110 --> 00:03:28,380
No.

58
00:03:29,480 --> 00:03:31,530
OK, and then let's see what happens when you click.

59
00:03:32,360 --> 00:03:38,510
So you can see each of them has a different type of message that will be sent so you can, of course,

60
00:03:38,750 --> 00:03:43,220
create different code to be executed, depending on which button was pressed by the user.

61
00:03:43,520 --> 00:03:45,380
So let's look at this alert dialog.

62
00:03:45,680 --> 00:03:46,670
How do we create it?

63
00:03:46,700 --> 00:03:51,170
Well, I have this button alert dialog with an on click listener.

64
00:03:51,170 --> 00:03:53,870
And then I called this the function alert dialog function.

65
00:03:53,870 --> 00:03:56,750
Does this a function that I created myself here?

66
00:03:56,990 --> 00:03:59,960
You can see private phone alert dialog.

67
00:04:00,320 --> 00:04:03,710
So what you need to do here is you, first of all, create a builder.

68
00:04:03,920 --> 00:04:09,650
So the builder class is there for convenient dialog construction, so it really helps us to create such

69
00:04:09,710 --> 00:04:10,460
a dialog.

70
00:04:11,000 --> 00:04:17,930
Then this builder is off type alert dialog the builder and you need to pose in the context, which is

71
00:04:17,930 --> 00:04:23,900
this because we want to show this dialogue in the main activity class or in the main activity itself.

72
00:04:24,590 --> 00:04:28,130
Then you set the title of the builder.

73
00:04:28,370 --> 00:04:30,110
So in our case, I called it alert.

74
00:04:30,650 --> 00:04:33,140
Then you have the builder message that you want to display.

75
00:04:33,170 --> 00:04:36,440
This is the alert dialog, which is used to show alerts and Arab.

76
00:04:36,800 --> 00:04:42,080
Currently, we're using this icy dialog alert, so you probably have seen this as we use it in the app.

77
00:04:42,680 --> 00:04:43,690
Let's open it up again.

78
00:04:43,700 --> 00:04:49,010
So this is this I can hear and I'm setting an icon, and you can, of course, use other icons here

79
00:04:49,010 --> 00:04:49,490
as well.

80
00:04:49,730 --> 00:04:55,520
So, for example, you can have input ads, which will then be a different icon.

81
00:04:55,550 --> 00:04:57,260
It will be this plus sign here.

82
00:04:57,860 --> 00:05:03,630
OK, so you can play around with this and then use the icon that you like so it can see you can adjust

83
00:05:03,630 --> 00:05:04,400
that as you want.

84
00:05:04,790 --> 00:05:13,460
And then you can see that the builder class has the method for a positive button where you need to create

85
00:05:13,760 --> 00:05:16,580
a text that you want to display on the button.

86
00:05:17,060 --> 00:05:25,400
And then in brackets there you have to well, in our case, use an expression which is a lambda expression.

87
00:05:25,410 --> 00:05:30,590
So we are saying OK, dialogue interface and which are the parameters that are passed in.

88
00:05:30,880 --> 00:05:36,390
And then we just want to execute a toast, which just has clicked.

89
00:05:36,410 --> 00:05:36,740
Yes.

90
00:05:37,430 --> 00:05:41,660
And also, the dialogue interface is what we dismiss.

91
00:05:41,660 --> 00:05:45,530
So this is the parameter that will be passed and you can see which is not used, but you cannot get

92
00:05:45,530 --> 00:05:45,980
rid of it.

93
00:05:46,280 --> 00:05:52,580
It's expecting it.So what we are just saying is, OK, just dismiss the dialogue, get rid of it.

94
00:05:53,000 --> 00:05:55,970
That's what we're doing with this dismiss button.

95
00:05:55,970 --> 00:06:02,360
So once we click on this positive button, the dialog interface dismissal will be executed and this

96
00:06:02,360 --> 00:06:03,470
will also be executed.

97
00:06:03,470 --> 00:06:04,850
So the toast will be executed.

98
00:06:05,150 --> 00:06:09,400
So you can pretty much add to code that you want to execute once somebody presses.

99
00:06:09,410 --> 00:06:09,740
Yes.

100
00:06:10,340 --> 00:06:15,800
And then we have a neutral button, which is the cancel button, how I call it there again, you have

101
00:06:15,800 --> 00:06:23,510
the lambda expression in here and I'm just saying, OK, dialogue interface and which are the parameters

102
00:06:23,510 --> 00:06:27,500
that are passed in and then you have a toast in our case.

103
00:06:27,500 --> 00:06:29,410
So that's the code that will be executed.

104
00:06:29,420 --> 00:06:34,670
And this is also the code that will be executed, which I just say, dismissed a whole dialogue interface.

105
00:06:34,940 --> 00:06:37,860
And then finally, we have this negative button, which is the no button.

106
00:06:37,880 --> 00:06:40,070
So what should happen when somebody presses?

107
00:06:40,070 --> 00:06:44,420
No, and there have the same structure, but you can, of course, write your own code here.

108
00:06:45,320 --> 00:06:52,580
Then finally, we need to create this alert dialog, which is of type alert dialog, and we used a builder

109
00:06:52,790 --> 00:06:53,990
that we have set up here.

110
00:06:54,200 --> 00:06:55,820
So we call this thing builder.

111
00:06:55,820 --> 00:06:57,320
We could have called that otherwise, right?

112
00:06:57,560 --> 00:06:58,880
But we just called the builder.

113
00:06:59,120 --> 00:07:00,500
And then we just say it create.

114
00:07:01,010 --> 00:07:08,240
OK, so basically what we're doing is we are creating the variable, which is, in this case, an object

115
00:07:08,240 --> 00:07:09,320
of the builder.

116
00:07:09,890 --> 00:07:12,920
And then we set up everything for this builder.

117
00:07:13,160 --> 00:07:19,880
And once we're done setting up the whole builder, we create it as an alert dialog and then we can use

118
00:07:19,880 --> 00:07:23,540
this alert dialog to set as considerable falls.

119
00:07:23,660 --> 00:07:28,250
So when that show allows, use it to cancel after clicking on remaining screen area.

120
00:07:28,640 --> 00:07:30,840
So let's test what that means.

121
00:07:30,860 --> 00:07:31,340
Let's see.

122
00:07:32,670 --> 00:07:40,710
When I click here surrounding this alert area, where this alert box here alert dialog, it doesn't

123
00:07:40,710 --> 00:07:41,280
disappear.

124
00:07:42,310 --> 00:07:45,370
So that is what we have set up with said cancel all faults.

125
00:07:46,330 --> 00:07:50,170
So let me set that to true and run the code again.

126
00:07:51,950 --> 00:07:55,940
All right, so there we are alert dialog, and then I click somewhere else you can see.

127
00:07:57,130 --> 00:08:00,010
Now we can just ignore the alert.

128
00:08:00,550 --> 00:08:04,690
So that's said cancel the ball and I'm going to set that false again.

129
00:08:05,380 --> 00:08:07,540
And then finally, of course, you have to show the dialogue.

130
00:08:07,540 --> 00:08:12,610
So we're prepared to learn dialogue, which is actually using our building to create it and then we

131
00:08:12,610 --> 00:08:13,990
show it here and this line.

132
00:08:14,380 --> 00:08:14,710
All right.

133
00:08:14,710 --> 00:08:17,410
So these are the alert dialogues and the snack bar.

134
00:08:17,440 --> 00:08:22,660
So let's look at the next one, which is going to be our custom dialogue function in the next video.

135
00:08:22,780 --> 00:08:23,260
See you there!

