Welcome back.
And this week, we are going to add the functionality which allows us to select the current location.
So sometimes you would want to have the current location of the user.
And I will show you how to get that.
And you can see once I press this button, it will ask for permission.
So, of course, we're using the location permission.
And then once we're done with that, we can then add the current location to this TextField.
So that's what we're going to do in the next video.
For now, we're just going to implement that button and also this functionality, which is the permission
request.
So let's get started.
Therefore, let's start with adding this little button here, which is not actually a button is actually
a text you.
So let's add that to our application.
So let's get the correct XML file that we want to edit, and it's going to be the ad happy place file.
And here, just on top of our image.
That's where we want to have it, so compared to Texas.
All right, just here.
And I'm just going to use this one here, so it's a text you I'm going to call this one TV selector
and location, it's matching the parent in terms of with it's going to rep the content.
That's a certain margin top10 a background, which is our shape, image, view, border.
So it has the same type of shape that our buttons have.
Then I'm using the foreground selectable item background, I'm centering it, then add some padding.
The tags should be a string that we define takes color will be color X and the text size will be also
a dimension that we assign.
And then what's important to this layout constraints.
And then of course we may not forget to assign the constraint for our combat image, which is now going
to be at the bottom of this new text view.
So bottom of.
Text to you select current location, of course, it's like all of the dimensions yet, so let's add
those two.
Let's go to the values and dimensions here.
And I'm just going to use those three dimensions here, which is at a place called Location Margin,
which is going to be five times the typical sort of current location, putting 10 density pixels and
current location.
Texas will be 16 S.P..
And then we had a string that we also need to add.
So let's go over to our strengths here.
And let's add a new string, which will be the text of that text view, which is the current location.
All right.
Now let's go back to that happy place.
Let's see if the arrows disappeared.
All right.
And let's look at the design and we can see now it's a set of current location.
The button seems to be fine.
All right, now let's go to the class, so to the ad Happy Place class and.
Then we can add some more deer, which is, of course, our unclick listener.
So here, let's add an electric listener for our new TextField, so it was a TextView.
It's like current location set on Click Listener.
We're just going to beat this.
Then we need to have a function which checks if we have the right to use the location of the user.
So I'm just going to create a new private phone, his location and abled, which will return a boolean,
hopefully.
True.
And then Volle location manager, which will be our guest system service.
And context, that location service, so here we are asking for the location service.
And then we need to return something, and that will be return location manager, that is provider enabled
with our location manager.
And here, maybe it makes sense to say that this is going to be a location manager object.
And then it understands it.
All right.
And now, of course, our get system service should be used as a location manager, so then it's going
to be fine and here's going to be the location manager as provider.
Or it's going to return location manager is provider enabled.
Which is our location manager taught network provider.
So we're checking.
Either of those two.
So this function will basically just tell us, do we have the location of the user or can we get the
location of the user or not?
And it's going to return.
True or false?
Now, of course, if you want to have the permission for the location, you need to add this to the
Android's manifest.
So here, let's see what we have.
So external storage, right?
External storage.
And we had access.
Find a location there already, even though we have never used it.
So let's add it just underneath, which will be the course location.
So he his permission, Android thought permission.
But of course.
Location, access, course, location.
All right, so these are the permissions that we have set up here for one location, which is a super
precise location and of course, location, which is less precise.
But sometimes you just cannot get the finest location, so it's going to fall back to the course location.
I know we just need to set that up in our ad happy place activity, so here we need to check do we have
access to the location or not?
And.
That should be, of course, tested once we click on the button, so let's go over to the unclick.
Function here, where we had all of those different conflicts and now we have the last one, which is
going to be underneath this one here, so it starts here are dot, dot TV underscored select current
location.
So if that one is clicked, then I want to check if the location has not enabled.
So is it not enabled?
If it's not enabled, then let's let the user know where the toast.
So your location providers turn off, please turn it on.
And then.
Let's start with an intent here, so let's say evil intent, intent settings, thought action.
Location source setting, so action underscore location settings so that we'll just go to.
The location settings of the app.
So the.
So the permission settings and then let's start the activity that we just prepared so intense.
And then.
Otherwise, we can go and check with Dexter so we can use Dexter here again with activity, with permission,
and here actually with permissions, because we can use multiple permissions.
So we're going to use the manifest permission.
But access.
On the score, final location, so here in the final location is the one permission and the other one
is manifest, that permissions that Hexis.
Of course, location, by the way, if this Dexter stuff and just with activity doesn't make sense to
you, then please check out the extra demo once again, because they are explained in depth what happens
here.
All right.
So then we can check the listener and create a new object here, an object which will be a multiple
permission.
Listener.
And which we have two functions, so let's just add them, so the two functions that we need here are
the permission checks and the on permission Rusciano should be shown.
So basically, it's going to look like this.
So we say with the listener, multiple permission listener, we have permission checked.
So if the report says that all permissions are granted, then for now we're just going to run a toast
here.
And the toast will say location permission is granted.
Now you can get the current location and then on permission reshingle should be shown.
So if the user didn't give us any permissions, then we're just going to show the dialog for permission.
So, so rational dialog for permission, which is this function that we created before where we said
it looks like you have turned off permissions required for this feature.
It can be enabled on the application settings.
OK, so that is something that we have set up earlier in this course.
That was when we were using the gallery and everything.
All right, and now we use the same procedure that we used for the camera and for the gallery, of course,
for our.
Location, so here, when we want to use the location, we use Dextre again.
All right, so at this point, let's test it and there we are, so let's go ahead and create a new location.
Let's click this button.
And you could see it asks us for permission, so let's allow it.
And location permission is granted and of course, what we want to do, instead of just writing this
little post here at the bottom, we want to, of course, get the location of the user and then populate
this location field here.
All right.
So that's what we're going to do in the next video.
So see you there.