Skip to main content

Filtering the Tasks by Location

Lesson 34 from: Developing Android Apps with Java

Tony Hillerson

Filtering the Tasks by Location

Lesson 34 from: Developing Android Apps with Java

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

34. Filtering the Tasks by Location

Lessons

Class Trailer

Day 1

1

Overview: What is Android?

19:56
2

Eclipse and SDK setup and build a Red Flashlight app

38:05
3

Ad a button to a Green Flashlight

21:06
4

Add the Green Flashlight

18:48

Day 2

5

Week 1 - Layout and Build a Task Manager App

04:10
6

Displaying a list of tasks

40:44

Lesson Info

Filtering the Tasks by Location

next step. Final step is we want to filter the tasks by location. So to do that, let's just put a little button down at the bottom of the screen. Um, that will actually toggle button, which will say whether we want whether the user wants to, uh, show only task that are nearby or all tasks. Um, To do that, we will cycle through all the tasks. And if the locations task is within a certain distance from the current devices location, then we will show the task. And otherwise we won't and also make some little layout changes to make this work. So that is, in the main layout, down at the bottom, paced in some code. We want to add a toggle button, which is something that she probably I haven't run into yet. At least we haven't in this class, so it'll be interesting to see. We'll call the toggle button show location tasks toggle, and it will work like a toggle button or like a radio. But sort of it'll be either in an honor are a check button. Sorry, it'll be either an honor off state, but it's...

ah, little bit of a different look for the user. Then we're gonna line that parent bottom, and we want it to be to the right of the removed button. And then we want Teoh have a hey, uh, bit of text that will show kind of a description of that button. And before, if I save, I think I should get an error here because this string doesn't actually show up. So see what? I have this in the cops to be. Show local tasks only. So I make a string that says that show local tasks only that I can use the re factor string. Yeah, that's good enough on what other thing? Just to give us some were room the text task of you, uh, or the add task. I'm going to just give us a little bit more room here. We could come back later, and and I don't know, change these things toe icons or something more a little more user friendly. But right now this will serve. I'm gonna change. Add task to be a plus plus sign. You add that to string, so open up strings. You know, at a string resource called plus sign, it will make that a couple spaces for patting on a plus sign, so that will give us just a little bit more room. And let's look at the layout and see if it actually shows something here. It looks like it's doing somewhere, um, some localization there. If, actually, is this to portrait? We don't have a quite enough room, but I think what is says on our office will actually give us some more room. So in any case, let's get that actually wired up. So I switched back to the, uh, view task activity, and I get a handle. Teoh those components down and set up views. I will have a handle on the toggle button and then I'll add that is a field. And then I'll add a click listener on the toggle button, which we will use to call a method called Show Local Tasks. And then it will pass a Boolean and that will basically a toggle button just acts like a button for most purposes. But it also has this is checked, comes concept, whether it's checked or not, and that will be a Boolean. So then it'll tell show local tasks, whether we want to show local tasks or not, and that will be a protected method. Let's leave that right here, and that will look like this. Let's just do based in some code here, let's do it like this. So if it's checked, we're gonna tell the adapter to filter tasks by the location and passing the latest location. And then we will give it a concept of a filter distance. Um, which I will create is constant here, and that is like how how close to consider close. That shouldn't be a string that should be a It's cold and in their long students along location filter distance will make it 204 200 meters. So that's how close will consider local or or Ah um yeah, local tasks. And then, if checked, his false. Then we'll say remove location filter. So it's going to the adapter and create this method filter task by location on the adapter to do it. Okay, get it. ISS Okay, so that will look like this filter tests by location. What I will do is he's paste in the code when we deduce is hold to Ray lists. One is all the tests and one is a set of filtered tasks, and then I'll just switch between those as the tasks that the location are that the task it list adapter is using to as its list of tasks. It's going to create a field for filtered tasks, and that'll be a new array list each time because we may start off fresh each time and then I'm going to go through each task. And if the task has a location, which once again let's look at that definition, it's just whether or not latitude and longitude or non zero. Ah, and then, um, if the task is within the geo fence of the location, every passing and the distance that we pass in the zone actually showing up is the same thing that I had here. Okay, so if this task has a location that is within the filter distance of the location that we pass in, um, then we will show it, and we will do that. This is the question that somebody asked just a little bit ago. How do we figure out a close something? This there is a class that, um, android calls location, hearing the paste in some code location is a sort of helper class toe. Let us deal with the the latte. Long's that air sent back by the location manager. Basically sweat location manager gives you here is a location, but it also has some helper methods. Um, and one of those is distance between and the way distance between works is it, either. Well, so what you can do is pass in an array of floats and make it as big as you want results. When you want the results to be and the results are here is look at the documentation. The results are set up like this. If, um, the results is only one, um, has only one position, then it will give you the distance. And if there if it's two or greater, the initial bearing is stored in results one, its three or greater than the final bearing is stored in results, too. So bearing is if you're moving, then the location will know what direction you're moving. And, um, if there was, apparently if there's ah, um you could have locations that have an initial bearing in a final bearing, and I'm not quite sure that the conditions that that will happen. But what we want is just We want to get both, um, everyone to set up with just one position. So we just want the distance. So again, this is a floater Ray that it will put our answer into, and then we'll just return, Um, the 1st 1 whether or not it's greater or less than the location. Distance will say whether or not this task is with him within range. So if the result that it brings backs basically the distance between these two points is less than the filter distance than yes, it is close enough for us to show up in the list and change. These titles are these labels here. So once again, location distance between six a Latin along and then another Latin along, and it says whether or not um, it says, what's the distance between those two points in meters, and it will put it into the first position of this distance array and then that will sell our adapter whether or not to put the the task into this filtered task, and then we'll set tasks equal to filter tasks, and then, um, we'll keep will hold the whole list of tasks in that will continue to be held in in this. Ah, um, see here we'll continue to hold the rest of the tasks inside the task array. Yes, Um, but we will do to remove the filter back here in the view task activity. We also call remove location filter if the check box comes off. So it's at that. That's what I didn't do. Yeah, I was wondering how that was gonna work. Where did I set that up? For some reason, I didn't set that up. Oh, I know what I did. I just have to pay since about a code. So, um, I wasn't quite making sense how that was gonna work. So to switch out away from the, um the filter tests that the unfiltered tasks we have to make some where to hold the unfiltered task. So I'm gonna create this other field, which is a realist of tasks and set that also equal to tasks. Um, the tasks when we create when we construct this adapter, remember, everything is run off of tasks, so we get the count off tasks and we get the items off of the tasks array and so on so on right there. But we'll always have a pointer at unfiltered tasks to the to the test that aren't filtered by locations than when we set tastic would've filtered tasks. Um, task will be pointing to the the the ones that are only close to this certain location. But then when we removed location filter all we need to do switched aspect to the unfiltered task and then notified that the data set is updated. So now we should be working. You run this. Okay, So now when I show local tasks, um, get milk should go away because it doesn't have a location When we blew up. Yes, that's right. I didn't actually, um, set up for the case just to keep the code simple. When we don't have a location, we can put in some extra checks for that to make a more friendly like, Hey, you need a location. Force close. This first thing I need to do is start this again. Oops. No messaging. Start up the task manager and then go to DDM Ass and give it a location. So send this and now we should have a location show only local tasks, and now they get milk should go away because it doesn't have a location. And then if I turn it off, then it will show all tasks and come here and add another One would say something like, Here's another famous address that I know Visit the White House at a location to that task. The address is 1600 again. Pennsylvania Avenue, Washington Do you see at this address? And it zooms us all the way over to the other side of the continent in Washington, and we could say Use this location at this task and now we have to location based task. There's a Google visit, White House, and if I shut off local tasks show local tasks. Only then it will see Google because we're near Google. If I turn this off, then we'll get those back. And so that is our application. Any questions? I like that little application. Let's find a deal of location because it's, you know, the real world. Especially fun to get away from the desk and go on field trips to test your application. Definitely, Tony. Real briefly. Can you try running that against London from the White House? to London. One of the vehicle Tween wave is the queen. What was that? Buckingham Palace, I guess. What would What's the address? Anyone Buckingham Palace would work. Wouldn't. Let's. Let's see if just if If Google smart enough will visit 10 Downing Street, 10 Downing Street. OK, We'll visit the PM instead of 10 cups. What is his name? Gordon Brown. Right at the moment 10 Downing ST. London. I don't know. See if this works. Whoa. Across the Atlantic. Nice works that fast. Soon to Wow. Yeah. Use that location task and he doesn't. We lost her location, so it blows up. All right? Anyhow, Yes, we could do that, too. And I didn't program and even that's all Google, right? I'm just gonna ask one quick question before we wrap up, and then we can save all our questions to the end. And that is how do we know when the GPS is has spun up and has a good location. Is your test that we can do? Well, um, I think the better way to do it. If it's not ready to give you a location, the location manager won't give you one. Um, so if you really need something. Anything, right? Right off the bat. You could use that. Get last location. A method that we looked at Forget get last known location. I can't remember what it was right away. That's that's what you could do to just get something. But that could be totally wrong. You never know. Um, but I would at least let you start working. But then, um, you could get access to both providers and then compare accuracy is they're coming back and see if you have something that's within the tolerances that your application needs, because each application may have different needs for how fine grained you want that accuracy to be.

Ratings and Reviews

a Creativelive Student
 

I thought everything on this site was free and now they want 99 dollars for a course

a Creativelive Student
 

very good

Student Work

RELATED ARTICLES

RELATED ARTICLES