Welcome back and this video, we are 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 to 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've 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.
I was available right from the start.
Snack bars were only added in the API level 23.
The activity is not required to be shown at Android when you want to run it.
So what can happen is you run toast, the 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 task cannot be dismissed by swiping.
Well, the snack bar can and 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 and for type messages to users that need attention.
So you can learn more about it here on the stack overflow post, you can just Google Android snack bar
versus toe's.
You can find some more information about that here.
All right.
Now let's get back to our code.
We have some other dialogue types that are interesting, even though the snack bar is not really a dialogue
type, some sort of output's 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 button set on unclick snack bar.
So here, very simple.
One line of code to create a snack bar, snack bar that make and then you have to pass it in the view.
You have to write a little text that you want to display then the length of the snack bar and then Duchow.
So very similar to what we have seen when it comes to Tostes.
All right, now let's look at the next one, we have alert dialogues here, so let's click on one and
you could see alert and there's a little icon.
This is alert dialog, 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 the cancel cancel operation.
Cancel once again the alert dialog.
Let's click on No.
OK, and then let's see what happens when you click, yes, 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 will 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 call this function alert
dialog function.
This is 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 this builder class is there for convenient dialog construction.
So it really helps us to create such a dialogue.
Then this builder is of type alert dialogue, that 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 dialogue, which is used to show alerts in our web.
Currently we're using this icey dialogue alert.
So you probably have seen this as we used 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 you 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 that you have
to, well, in our case, use an expression which is a lambda expression.
So what you're 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 to 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 dialogue interface dismissable will be executed and this
will also be executed.
So the those 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, dialog 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 this is the 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 are the same structure, but you can, of course, write your own code here.
Then finally, we need to create this alert dialogue, which is of type alert dialogue, and we use
the builder that we have set up here.
So we could be called this thing builder.
We could have called it otherwise.
Right.
But we just called the builder and then we just say 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 dialogue and then we can
use this dialogue to set as cancelable false.
So we're not shall allow user 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 or this alert box here, alert dialog, it doesn't disappear.
So that is what we have set up with said cancelable false.
So let me set that to Drew and run the code again.
All right, so there we are, alert dialogue, and then I click somewhere else, you can see.
Now we can just ignore the alert, so that's said cancelable and I'm going to set that false again.
And then finally, of course, you have to show the dialogue.
So we prepared to alert dialogue, which is actually using our builded to create it, and then we show
it here in 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.