Welcome back.
In this video, we are going to implement the functionality to select a date from the state picker,
as you can see here, so we can then select one of the dates press, OK, and the text will be populated
here depending on the format that we set.
So I'm using the German format here, for example.
Twenty third of January.
Twenty twenty.
All right.
So let's get started.
The first thing we need to do is to make the field non focus.
So let's go to our activity at every place.
And in there we have added text fields, plenty of them, and I want to find the one with the dates.
So here we have this added text and I'm going to add.
The two properties focus, so I'm going to set that falls so we cannot focus this at text and then also
focus in touch mode.
We're going to settle the falls as well.
So now we cannot click on it anymore in order to select it and had a date in there.
So we will not be able to just add a text in there.
But what we're going to do instead, of course, is going to implement our own code, which allows us
to open the data picker.
Whenever we do that, we need to have certain variables.
So first of all, a private variable, which I'm going to call standing for calendar and will be calendar
get instance as the name, OK?
And then we need to import to calendar class.
So I'm going to use the Java util calendar here.
And then I need a date set a listener so here private, and this one will be a late edit variable.
So I'm going to initialize it later on and I'm going to call that one date set listener and it will
be of this type data picker dialogue on date, said listener.
All right, now and the uncreate.
That's where I'm actually going to.
Initialize it, so here, let's add a date listener initialize here.
So I'm going to use the data picker dialogue thought on that, said listener.
So it's very similar to I said navigation on Click Listener.
So this one said to navigation on Click Listener or a click listener here, but here we are actually
waiting for someone to set a date.
So we are opening the data quicker dialogue, and then we are waiting for the user to select the date.
OK, so that's why we're going to use this on dates, that listener, but this time with the curly brackets.
And what I need to pass in here is to view and actually maybe it's easier if we.
Just this one here.
All right.
OK, so it gave me everything now view year, month, day of month.
So instead of me having to enter all of those manually, I'm just going to let it help me.
And now I can go ahead and set those.
So I'm going to set the calendar.
Set calendar that year has been the year, whatever is selected by the user and then I set that month,
so calendar month and March part month, which will be the month of year and actually this one month.
So I'm just going to use month.
And here, of course, is the month with capital letters.
OK, so you can see here we have many different options when it comes to the calendar that we can set.
And finally, I'm going to set the day of month, so set calendar that day of month, which will be
our day of month.
And why do we need to a day of month here instead of just say, well, it's because they could also
be the day of the week, for example, which would be Monday, Tuesday and so forth, or is it the day
of the month, which is going to be, for example, the second or the third of a specific month?
And then what I want to do is.
To make this whole class and all click, listen or follow the instructions of The View dot on Click
Listener.
All right, so an import view here, and once I do that, you can see that my ad happy place activity
requires me to implement the members, which is to unclick method.
OK, so and I have this unclick method and in here I'm going to take care of all the different unclick
events because I'm going to have plenty of them.
And yeah, I just want to do it another way this time.
So instead of always having the click listener here.
So for example for the save button save button save set on Click Listener instead of doing it here,
I'm just going to have this on Click Listener up here or down here with on Click.
OK, so the click listener is my whole class, but this unclick is going to be where we implement the
whole code for it.
And the way we do that is with a one key.
So here we look at the view and it just give me this view name, which is the V.
So whatever view that was clicked on is going to be this V, OK, and I'm going to check the ID, so
I'm going to see if the ID is for example, or that ID.
And here you can see the different elements that we have.
So if it's the date so added text date which means.
This.
One here, so if somebody clicks on this app, compete at a text, then I want to run some code and
we do that with a lambda expression here so we can see when somebody press this button, I want to execute
some code.
So I'm going to open up to date Pechora dialogue.
Which allows me to then select a date.
So here I need to pass in the context and I'm going to say this at Happy Places or prospectivity is
going to be my context.
And then we need to present the date set listener.
So the listener and I'm going to use to date, said listener, that we prepared earlier, then we need
to pass in this year.
So calendar dot get.
Calendar, but year.
Then we need to have the and actually it's year with capital letters.
Then Callander thought, get.
And this time it's the calendar that month and finally calendar get calendar, but they have month.
OK, now this is a pretty long one, so let's put that into different lines here so that it's a little
more readable.
OK, so we have this state picture dialogue.
What we need to pass in the context we need to pass in the data set listener or a listener, take listener,
and then we have our different date specifics for such as the year, the month and the day of the month.
And then finally, I want to show the state picture dialogue.
So here we need to add show.
All right.
That by itself is unfortunately not enough, because we also need to make sure that our edited text.
Is actually following this on Twitter listener, so it states that set on Click Listener, and then
I'm just going to say this.
Why this?
Well, because this class are at a happy place.
Activity is our only listener.
So it's on click event here or click function, which takes care of the whole unclick events of all
of them, is going to take care of this one as well.
So just say OK at this, they're OK now we can test it, even though that's not everything yet.
Of course, we still need to change the text and actually put it into the added text.
So let's see, let's click on here and then click on the date and you can see the picture opens up,
I click on OK.
And of course to date is not have entered in there.
So that's the next thing we need to do.
And I'm going to do that with a function, so I'm just going to create a new function here, private
fun called UpToDate in View.
And in there, first of all, we need to have a format.
So each time we're working with dates, you have to have a certain format.
So I'm going to call this one my format and I'm going to use the day and the year, which is the German
format.
Of course, you can use another format here, if you like.
So month, month, day, year, year, if you prefer another direction.
OK, and then we need a simple date format.
So Val ASDF is going to be our simple date format, which will take the format that we created.
So my format and translated.
So local dot get default.
So it's going to get the default date format of the local.
So of the settings of your phone and then etty on this court date set text.
So now we can go ahead and set the text and how do we set the text?
Well, I'm just going to use our ASDF, so our simple date format and I'm going to say please, former
to date of our call dot time.
So just use the time of our calendar.
And of course, it's not going to be a string yet.
So I need to make it a string by using the two string method here.
All right.
Now, update and view, of course, needs to be called at one point.
And what would be the best point for it to be called?
Well, I would say.
Once we selected the dates, and that is in our own data set listener, so here, once we're done with
all the rest, we can go ahead and update the date of you.
OK, so once we have the calendar, because in the update view method, we are using the calendar time
and.
It's good if we said that before, and that's what we do, we said it.
That's why it's also important that Kjellander has a global variable here.
Or a field.
All right, so let's get started.
So there we are.
Let's start.
Let's click the date, let's select a date, and you could see it's pasted in there.
All right, perfect.
So that's our date picture.
And then the next video, we're going to add some more features.
So see you there.