Welcome back in this video, we are going to implement this data picker here, so basically this little
dialog or this pop up that you can see.
So this is called a data picker dialogue where you can then select a date and click on the OK or cancel
button.
And depending on what you select, an event will be triggered.
OK, so let's see how we can implement that into our application.
Therefore, we need to make sure that we get the right button because we want the button to execute
something.
And we can get the button that we're talking about to buy its I.D. so you can see, in my case, the
Buttons I.D. is called Button five.
And you can go to the designer and click on the button to see the I.D. here at the top as well.
And I want to rename it.
I'm going to call this one button state picker.
OK, so that's the idea that I want to use for my button.
Now the main activity and you can find the main activity under Java.
Then your package name and there is the main activity.
Here is where I, first of all, need to set up the buttons so that I can use it in my project.
And therefore, we need to use an approach that we've seen before where we give the.
Button that we want to use a name, then we say what type it is in this case, it's the tip button and
we need to input button for this to have work.
So here import Android widget button and then we can find it by its ID.
So find the by ID, and then we can use our Dart ID and then to name that we used.
So here, by the way, it's not this name.
This is a name that you can call however you want.
It is important that this part here is the actual ID.
OK, so now we're finding this button based on its ID, and we can now use it by using this variable.
So we basically need to create this variable in order to now have access to the user interface item
in the XML file and do cool stuff with it, for example, and on click listener, as we had seen before,
when we were creating our people counter.
So here we can just go ahead and use this button and call it set on click listener method.
The one with the curly brackets here and then in there, we can now run the code that we want to execute
once the button is clicked.
So for now, I'm just going to use a post here that make text where we need to pass in the context,
then the text that we want to display.
So button date picker pressed.
OK, so this will be the text that will be displayed, and then we need to define how long this should
be displayed.
And there is this length long fixed value.
And then finally, we need to show it.
OK, so let me put that into a separate line.
So we have toast make text where we pass in the context, which is this, which is our main activity
because that's where I want this toast to appear.
So on the screen of the main activity, then the text that I want to be displayed and then I'm just
going to say toast length, what a long.
So it should be displayed for a longer period and then I need to actually show it by calling the show
method here.
So let's test this with our application and to see if this is working.
So because they're at the bottom, the toast appears.
So select date clicked and button data picker press was executed.
So this just shows that our button works and this is just a good practice for you if you implement the
new button to see if everything else is set up correctly because otherwise, well, you could develop
some huge code base and not use it and not tested before that.
And you wouldn't know what the problem is.
You would think that it's the code that you added after the fact, but maybe it's already the code that
was all the way up to here.
OK, that's why it's good to test a toast to see if it works OK?
So now let's go ahead and do all of the data picker code.
And in order to run data picker code, I'm just going to create a little function here, which will
take care of that.
OK, so this function will be a separate function that, well, we will call from the set and click
listener.
So we're going to call it inside of here.
So let's go ahead and create it, and I'm going to call this one.
Click.
Date picker.
OK, so once the date pickers clicked, that's when I want to call this method so we can just call it
in here directly.
You can see like soap and that will already execute whatever is inside of this method.
So now let's cut this.
Tossed out of there and just put it in here and see if this is going to work.
And let's click on the button and you see it works, so now internally inside of this data picker button,
it calls dysfunction here and then it executes whatever is inside of this function.
So whatever is in here and in this case, it's just a toast for now.
OK, so in order to use a date picker, we are going to require a couple of parameters.
We will need to have the year, the month and the day of the month, for example.
OK, you can see that by using the date picker class.
So here you can see there's a see here, which just means that this is a class.
So if we use this date picker dialogue now, we're using the date picker dialog class and you can see
there are four different constructors that this date picker dialog has for us.
One word just needs the context, then one where it needs the context and the theme race ID.
And then the third one, which is the one that we're going to use where we need to pass in the context.
Then the lesson there, which is of type, date, picker dialog on dates, that listener, which is
an optional.
So you have to put it in, but it can be null or it cannot be null, then you need to pass in the year
as an integer to month as an integer and the day of the month as an integer.
OK, so let's prepare all of those variables before we actually use this date picker dialog, and the
first one will be, as I stated earlier the year.
OK, so in order to even get the year, we need to create a calendar.
Object.
So my calendar is I'm going to call it, and I'm going to use the car lender.
Don't get instance.
So that's how we can get the calendar from Java due to this will just give us access to things like
the current calendar dates like year month, day of the month or so forth.
I can now go ahead and create the year variable where I can use the calendar to get the year.
So here can just pass in calendar year, so my calendar gets.
And then it uses the calendar and then it can retrieve the year for us, and then we can go ahead and
do the same thing with the month.
So my calendar don't get calendar dot month.
And now there comes the tricky part with the day.
So we need to pass in day to day and my calendar don't get.
And here it's called calendar dot.
And there's no such thing as day.
There's only day of the week, day of week and month of year.
But the one that I'm going to use this day of month here.
So the day of the month would be, for example, the 31st of May or whatever.
OK, so that's the day of month.
OK, now we can actually use this date, pick a dialogue and pass all of the details that it needs.
OK, because if you look at it and the idea can help you here, you need to pass in the context.
And we're just going to pass this because we're going to want to display the state picture dialogue
in our main activity view.
Then we need to pass into date picker dialogue listener.
And I'm going to put that in a separate line because otherwise it will be difficult to read.
So here we can use date picker dialogue dot on a date set listener.
OK.
And here there are different types of on dates that listeners similarly to what we've seen with second
clip listeners.
We can either used to run the brackets or the curly brackets and just say what we want to be executed.
And this on dates.
That listener gives us a bunch of data.
So it gives us the view, the year, the month that was selected by the user and accordingly, of course,
also the day of month.
OK, and this is going to pass all of this details to us.
And now we need to pass into and does this, by the way, going to be one too many?
OK, now we need to pass in the year, the month and today.
OK, you see, suddenly the arrows disappear.
So this whole thing here is just one parameter we're passing.
So it's an unpaid set listener.
And now after this error, we can do whatever we want to execute.
So similarly to what we had here with our set on click listener, by the way, we get a view here.
So you could have just used view and then money's greater then.
So this arrow and then use that view in here.
So here use view.
And this view would then contain the details about the view that was used for the button on Click Listener.
But because we're not using it, we can remove the single lambda parameter declaration.
OK, so what?
Using a number here, but because we're never using this view itself, we can just leave out the code
then in order to go into this in more detail.
It's I would say it's a little too early in the course yet.
But basically what's happening here is we're getting four parameters.
It's like you're creating a method that needs a view of type, view your needs a year of type and it
needs another, well, month, for example, of type, end and so forth.
So it would be as if we were using all of that and we were were given all of these details once this
method is called and that's what's happening here.
So as I said, it would go a little too far, but we're basically using a lambda expression and you
see, they're all underlined because we're never using them.
And the idea tells us, Hey, you're not using them.
So instead, you could have just used an underscore, for example.
So you can use underscores for variables that you're not using that are given to you.
But as I said, that would be a little too far for now.
So now what can we do with all of this knowledge?
Because it's all good, Dennis.
But.
Like, I need to see something.
Well, that's what you might say.
So let's use this toast here, and let's put that in here.
And let's just see if the date picker works, so let's replace this with date picker works.
So this will only be executed this toast here.
If this on dates set listener actually worked and then in order for us to even display this date picker
dialogue.
So we need to use to show method here in order for this to even do anything, because otherwise we have
all of this beautiful code set up, but we never actually show the date picker dialog.
OK, so now let's go ahead and test this and see if this is going to work.
So let's click on it and we see the date picker popped up, and now let's to cancel and you'll see nothing
happens.
Now let me press OK, and now you see they picker works.
So this will only be executed if we actually select it to date.
So once I select any date here and click on OK, I will have selected the date.
But in order to select the date or take the selected date and actually displayed in our application,
I would say, let's go over to the next video where we're going to implement that.
And by the way, you can see that this code here is feinted.
So this is how you would have clearly stated what's up.
But you can simplify this by actually even getting rid of this entire statement here, because it will
know, based on the details that we passed to it that this is going to be a date picker dialog on date
set listener because that's what the date picker dialog needs, right?
It expects this and then the format fits and all of that.
So that means that you can actually get rid of this statement here and shorten the code this way.
OK.
This is going to be a little less readable in a way that you don't know specifically what's up there.
But if you know, OK, well, we're talking about a date picker dialog and you're a little more experienced,
you'll understand, OK, well, you will need to display the on date settle.
Listen there in here, and that's what this part of the code would do.
And of course, you can just rerun the code to see if it still works.
So just confirm that it still works and you can see that they are still pops up and that still says
date picker works.
All right, so that's it for this video.
See you in the next one.