Welcome back.
And this video, we're going to look at dialogues and dialogues are going to be required for further
projects that we're going to build and we're going to use them every now and then.
And that's why it makes sense to have a deeper look at them because we're also going to require them
for this project that we're currently working on.
So let's get right into it.
So what I have here is a project, and I'm not going to build everything here in this video.
I'm just going to show you two different types of dialogues that there are and how you can build them
in code.
And yeah, let's just get started.
So first of all, we have this little image button, and once I click on it, you see, I get this text
here at the bottom.
So this text is not as we have seen before, a toast, but this is a snack bar.
So I can click multiple times on it.
And as you can see, it's constantly replacing the old one, so it's getting rid of the old snack bar
and then it's adding the new one.
And also you can swipe it away.
So here again, swipe it away.
OK, so this is a snack bar.
How does a snack bar differ from a.
Toast, you might wonder.
All right, let's look at it.
8OZ was available right from the start.
Snack bars were only added than the API level 23.
The activity is not required to be shown in Android when you want to run it also.
What can happen is you run it.
Most activities closed and toast still persists.
All right.
In snack bars, that's not the case.
So the snack bar will only be visible in the activity of the application.
It can't be, or the host can perform an action based on user input.
In the snack bar, you can set something up.
So if the user clicks on the snack bar, then you can run some additional code.
The toss cannot be dismissed by swiping well, the snow part can and toes cannot handle user input like
a swipe, etc. snack bar can, and the toast is good for showing and for messages to user.
And the snack bar is good for showing warning info, type messages to users or that need attention so
you can learn more about it here on this Stack Overflow post, you can just Google Android snack bar
versus toast.
You can find some more information about that here.
All right, now, let's get back to our code.
We have some other dialog types that are interesting.
Even though the snack bar is not really a dialog type, some sort of output that is visible to the user.
All right, now, let's look at how we can create such a snack bar.
Well, we have this image a button set on click snack bar.
So here, very simple one line of code to create a snack bar, snack bar to make.
And then you have to pass it in to view.
You have to write a little text that you want to display, then the length of the snack bar and then
Dutch shell.
So very similar to what we have seen when it comes to toasts.
All right, now, let's look at the next one.
We have alert dialogues here, so let's click on one and you can see alerts and there's a little icon.
Does this alert dialogue, which is used to show alerts on your app and you can cancel say no or yes?
OK, now how do we create such an alert?
Well, first of all, let's click on to cancel.
Can see Click Cancel Operation Cancel once again the alert dialog.
Let's click on.
No.
OK, and then let's see what happens when you click.
So you can see each of them has a different type of message that will be sent so you can, of course,
create different code to be executed, depending on which button was pressed by the user.
So let's look at this alert dialog.
How do we create it?
Well, I have this button alert dialog with an on click listener.
And then I called this the function alert dialog function.
Does this a function that I created myself here?
You can see private phone alert dialog.
So what you need to do here is you, first of all, create a builder.
So the builder class is there for convenient dialog construction, so it really helps us to create such
a dialog.
Then this builder is off type alert dialog the builder and you need to pose in the context, which is
this because we want to show this dialogue in the main activity class or in the main activity itself.
Then you set the title of the builder.
So in our case, I called it alert.
Then you have the builder message that you want to display.
This is the alert dialog, which is used to show alerts and Arab.
Currently, we're using this icy dialog alert, so you probably have seen this as we use it in the app.
Let's open it up again.
So this is this I can hear and I'm setting an icon, and you can, of course, use other icons here
as well.
So, for example, you can have input ads, which will then be a different icon.
It will be this plus sign here.
OK, so you can play around with this and then use the icon that you like so it can see you can adjust
that as you want.
And then you can see that the builder class has the method for a positive button where you need to create
a text that you want to display on the button.
And then in brackets there you have to well, in our case, use an expression which is a lambda expression.
So we are saying OK, dialogue interface and which are the parameters that are passed in.
And then we just want to execute a toast, which just has clicked.
Yes.
And also, the dialogue interface is what we dismiss.
So this is the parameter that will be passed and you can see which is not used, but you cannot get
rid of it.
It's expecting it.So what we are just saying is, OK, just dismiss the dialogue, get rid of it.
That's what we're doing with this dismiss button.
So once we click on this positive button, the dialog interface dismissal will be executed and this
will also be executed.
So the toast will be executed.
So you can pretty much add to code that you want to execute once somebody presses.
Yes.
And then we have a neutral button, which is the cancel button, how I call it there again, you have
the lambda expression in here and I'm just saying, OK, dialogue interface and which are the parameters
that are passed in and then you have a toast in our case.
So that's the code that will be executed.
And this is also the code that will be executed, which I just say, dismissed a whole dialogue interface.
And then finally, we have this negative button, which is the no button.
So what should happen when somebody presses?
No, and there have the same structure, but you can, of course, write your own code here.
Then finally, we need to create this alert dialog, which is of type alert dialog, and we used a builder
that we have set up here.
So we call this thing builder.
We could have called that otherwise, right?
But we just called the builder.
And then we just say it create.
OK, so basically what we're doing is we are creating the variable, which is, in this case, an object
of the builder.
And then we set up everything for this builder.
And once we're done setting up the whole builder, we create it as an alert dialog and then we can use
this alert dialog to set as considerable falls.
So when that show allows, use it to cancel after clicking on remaining screen area.
So let's test what that means.
Let's see.
When I click here surrounding this alert area, where this alert box here alert dialog, it doesn't
disappear.
So that is what we have set up with said cancel all faults.
So let me set that to true and run the code again.
All right, so there we are alert dialog, and then I click somewhere else you can see.
Now we can just ignore the alert.
So that's said cancel the ball and I'm going to set that false again.
And then finally, of course, you have to show the dialogue.
So we're prepared to learn dialogue, which is actually using our building to create it and then we
show it here and this line.
All right.
So these are the alert dialogues and the snack bar.
So let's look at the next one, which is going to be our custom dialogue function in the next video.
See you there!