Skip to main content

Adding a Location to a Task

Lesson 24 from: Developing Android Apps with Java

Tony Hillerson

Adding a Location to a Task

Lesson 24 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

24. Adding a Location to a Task

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

Adding a Location to a Task

So Step one, we're going to add. Start adding a location to our ah task by on the task screen. When you're on the ad tasks during when you're about to add a task, you can tap a button that says at a location to this task, if you'd like and then that will take you to a map for you, where you can enter an address and then look for a specific location that you want to add. And then that will return that location back to the ad task screen, where it will then be ready to attach to the task. And then next next class. We're going to save that in the database and then actually use that as we get Mawr location aware on the device. So the steps we need to go through are we need to add a new button, and then we will ah used a new method on an activity called start activity for result, and I will explain that a little bit more as we get into the code. But basically the idea is that we're going to start an activity expecting a result to be returned instead of, um, as we've done in the past just ju...

st starting activity so that it runs and we're not really We don't really care what happens in there. We just come back whenever it finishes. In this case, we want to finish and have a result return to us, and that result is gonna be what's called an address, which we will then make ready to attach to the task. So this is the flow. You start the activity for the result, and then the activity runs. You do some things interesting things. And then when the activity is done, it will call back to on activity result in the calling activity, which will be the ad task activity, and then that will have the result, um, available on the intent that you can you can grab off the intent and then use it, and the result of general will be an address in this case, and we're not actually gonna build the map screen just yet, But I'm showing it for for the purpose of understanding what the flow is gonna be like. So let's get into the code, and I'm going to start with the project that we left off with last week, and we're going to start by building a, uh, the changes to the ad task screen, which again, in the emulator looks like this. So we're going to add a button here. This is at location to task, and when we click, it will go to a new activity. You look at this error has occurred. I get that a lot inside of the the visual editor for editing XML from layout files. This is that's one reason why I don't use it that much. So I'm going to switch over to the XML view, which doesn't give me years quite so often. And when they when it does give me air there errors, they're probably my fault. Ah, the first thing that we need to do is added button. So I'm going to put that remember, we're using a linear layout, so everything is just gonna be laid out. Top to bottom text, view, edit text, and right underneath the edit text. I want to add a button which is going to be the one that will click to add location someone, a paste in some code to save typing. I want the I d to be at location Button. Uh, Phil, parent rap content, just like the other buttons. And then the text should say something like add location to task so I can use our re factory and feature to say what I want to the title to be, and I can select it and say, Re factor extract android string. And that will be exactly what I want. So now I should have a button on the screen. Ah, to make it actually work, we need to add a click listener. So, um, actually, I have, I think, in the code. Um, I've meant to do this at some point. I'm just going to do it now. I'm gonna show you a different way to do a click listener. And this will not be in the sample code that I uploaded to, um, that's available through the O Reilly. Um, zip file that you get if you buy the course or through my get hot project. Um, but just on a whim today, since I'm feeling experiment, if I'm going to show you a different way to do Click Listers, which is a little bit less verbose. So on the at task activity. What we're going to do is add a function that you will call from this button and you can declare which function you want to call right on the button instead of setting up a click listener on the button inside the activity, which is a little more verbose because you have to do the whole, um ah, creating anonymous class and all that kind of stuff of you on click listener and then implement of, ah, a certain method. But what you could do instead And this is a new feature in 1.6, I believe is you can say android on click inside the XML, which is pretty cool. And then you need to say the name of a function and will will, um, call this function, add location button clicked. And that's all you need to do inside the XML. Now, inside the activity, I'm gonna open up Source. Comradely android Task manager. Add task activity and what I need to do. Sure why this guy keeps popping up when I need to do is at a, uh a method called ad location button clicked, and it needs to accept a view, and I think it needs to be public public void at location button clicked. That takes one argument and of the type of you. And now this button will be clicked when her this dysfunction will be called when the button is clicked. And that saves a whole lot of typing and a lot of weird on Kloeckner listener. Stuff like this, although it does have one drawback, is that it does make it a little less clear. Um, what's actually going on? Because here I'm saying like this this button when you click it, this is what I want to happen So it's very, very clear that I'm associating the button with the function, um, to alleviate that just a little bit. I've used a naming convention, which makes sense when you read it at location, button clicked. That means that I expect the ad location button to call this function her this method. So in any case, that will save us a little bit of typing. And it's something that you can use and what I want to happen then and, ah, when we paste in the code from my sample code, which, um, which you will have access to and, um, there will be something slightly different. So let me get to a second after import these things. It's not giving me the option to import for some reason. So we go up here, I think it is import Android That content, I think that intense. Is that right? Android dot content dot in 10th. Yeah, and I'm not sure why. Eclipse wasn't finding that. Um, I was just worried for a second that something was really wrong with my environment. It doesn't seem to be anything wrong with my environment. It just may be the errors were such that, um it would just not looking in the class path yet. And it was complaining about others first. Anyhow, intent. We've seen that many times before, so I kind of know how that looks. So now they have intent imported. I can talk about what comes next. So I remember we have to kind of jump through this weird This, um, sort of ah, red tape to get the actually What? What this We, uh we mean to refer to when we use a click listener like something like here. Oh, are well, in other examples, we've seen where you have to refer to this. If I go to through this method of, ah, of actually having the declaring the method that I want to call from the XML, then I don't have to do that anymore. And it's just like you're probably used to. We could just refer to this as the context or the activity as the first argument to the intent. So what The intense I want to be, um, doing here is opening up a new, um, activity called ad location map activity. And so I've declared that here and then I'll go through and created in just a second. Um And then instead of calling start activity, which we've done a few times in the past to start a new activity, I need Teoh call start activity for result. And then what I'm gonna do is passing the attempt just like you would for start activity. But then I want to have a constant request choose address. That will be something that will mark the type of intent, Um, that will be sent back from the previous from the class that I called out to or from the activity that I called out to, um the map activity in this case, which will call back with on activity result. And then when the intent comes back to a special method that will implement here in a second, um, it will come back with this type of request this constant in the in the method signature show I had no or than the arguments to the method. So I'll know what attacked activity is coming back. And then I can choose based on this constant what I want to do based on, um What activities coming back that way, I can call it a multiple activities having induce and work and give me back, um, the results of whatever work they did. So we look at that in just a second. We're going to see this a couple different times this different parts of this process. Um, so hopefully by that the and we'll see how that all comes together. But if you have any questions, start thinking about them now and then see if I answer them along the way. So, first of all, I'm gonna, uh, implement this constant right here. Somebody created constant call. It requests, choose address, and that needs to be of type int. So fight. Um, Tab up here. You can see I've got the block box surrounding it because I use the eclipse generation method. Um, and then I can choose things about it. Private static Final is fine. Questions address zero is fine. All we need to do is make sure that's a unique integer so that we know the different request types that we have, Um, outgoing and incoming. Now I can click on this. There's this little, uh, get her over here on the side. You could see how my mouse pointer is. That shows me errors in the project. I can click on this and jump down to the error, which is this activity that doesn't yet exist that we need to create. So now we'll go create that activity will just have a simple activity. Um, so that we can see that fingers are running. And then, um, and the next step will be implementing the view there. Someone, a credit class, at location, map activity, clicking there. I want this to be of type activity. Some reason I can code completion in there so I can browse for that super classic selection. It seems like there's something wrong with my environment. I'm not sure why this happened, but it's not looking for any of the android classes. So I'm just gonna have to fix that in the code eclipses and helping me out here. I'll just leave. This is a placeholder because you can't find it. So click finish. And then it will create the class for me. And this Properties thing keeps popping up. I'm not sure why. Okay, so sometimes eclipse can really annoy, but that's surmountable. So in activity as I go back to add task activity and they just need to figure out what what class it's in or what package that's in. Um, I can hit have three on this task manager activity because that should import activity its android dot app that activity. So I can copy that. Close this and come here and replace this faulty import with the correct one. And now everything should be fine. Um, and we will change this and a little bit to map activity. But right now, right now, activity is just is good enough. Okay, so now we have an activity. Um, that is capable of taking an an intent on being come started by the android framework. So that's something we can call out to when this button is clicked. Um, but there's one other step that I needed. Of course. Can anybody remember what I need to do If I'm gonna start a new activity with an intent so that things don't crash? I've got some guesses here. Update the manifest. That's right. So I need to g o to the manifest. By the way, in this open resource pain, which we've used a couple of times again on Mac, that's command shift are on Windows. I think it's option are all shift our for open resource, which will open. Resource is in your project. You can use stars and question marks and things from like pattern matching expressions to help help search for things. Second search for star em, and I'll find things that start with a capital limbs or have a couple of them somewhere in them. Mathis is one of those. I can double click that open the manifest. They get this editor here, but I'd rather go out at the XML and underneath the at tacks activity all at another activity. Add location map activity and the text should be, um, advocation to task metal show up in the in the title. So now this is already to run. Why don't we just do a quick smoked us to make sure that my button shows up and is clickable on goes to the right activity a task. Now I have add location to task. Click it and it goes to this other activity which doesnt have you yet so doesn't show anything. I could go back. OK, so that's that's the flow that we want. But there's one more piece that we need to set up inside add task activity, which is the callback piece. So now we're setting up this intent to call out to the new activity to map activity and we wanted to come back with the results. And when it comes back, the result it will pass back in this constant. This request choose address. Ah, where we wanted check to see if this is the result that is coming back is in a special method on add task activity that is called on activity result. That means that method will be called whenever an activity that we start with start activity four result, um, finishes and comes back and and gives us our results. I'm gonna paste in the code that we want here, underneath, on create and then talk through it. So the method name is on activity result and that will be overridden from activity. And it takes three arguments, request code, result code, and then it passes back of the intent that started everything off, which is where we put data that we want to pass around between activities. So it's intense. They're not just for starting off other activities there also for holding data that can be, um, information that's useful between the two activities. So sharing data between runs of an activity and what I do here is I check if requests choose address. Remember, that's the constant that we just implemented up here is the request code. So it means this activity is gonna finish and send back the request code that started it. And if it's request to his address and the result is okay, those air, that's one of ah, think two different types of results that that an activity can finish with, and I will show you how that works in a second. But you can either say everything's fine and I'll send back this This code as the result, I think you can say canceled is another one. Um and I look at that in a second How we implement that on the other on the other side of the equation, the other ad location map activity. So basically, I mean, this is nice. It kind of reads like English. If the result. If the request was choose address and the result is OK, then we want to get the address off of the intent which is in this local variable data. And the way we get that out is there is this concept of extras on an intent. Extras are where you store data that you want to pass between, um, activities on an intent and there are a couple of different types of extras that you can add. First of all, all the job of primitives can be added, strings can be added, and then if you have more custom data, you can do one of two things. You can either make just simply market serialize herbal. Um which is a A, uh, Java interface that you can implement that doesn't have any requirements on methods that you need to implement. And then you can pass serialize herbal objects on intense between activities or android also has this concept of making a class parcel herbal, which is an interface that you need implement, and then you need to do a little work in there. We're not gonna go through that in this class, But a lot of the android, um, or the objects that ah, you'll use that are part of the framework that you would end up passing between intense like, for instance and address are already made possible, so they're ready to go. So what we need to do is get this parcel extra out of the intent and it's going to be of type add location. I'm sorry, the key. So this is like a key value pair type of a situation. So the key will be this constant that will set up on add location map activity called address result, and that will be a key that will point to the address that we need to grab off of the intent. So let's just create that constant really quick. That needs to be a string, and we'll just call it something like address. It just needs to be unique String s so that when we finish this activity will put put the address that finished that we grabbed from the users activity onto the intent with this key. And then as it returns to the calling activity, add task activity will be able to grab it off using that same key. And then everything will be kosher and will understand without having to Teoh maintain bear strings to get those keys out. Um, what the key name should be And then finally, I want to put this on a field so that I can use it. Um, once everything is all ready and this activities returned and now I have an address. So I'm going to just say, create field here, which should be of type are syllable, but we want that to be instead of type, address, hip and I get no completion. Have to look into that later so I can and I just copying that in So what we want that type to be is of android dot location dot address. We could get rid of this unused partial import and Now that is the full. That is the skeleton that we need in place for that round trip between these two activities. So once again, that's gonna look like kick off this activity by adult adding location, a task that will start the map activity. And when it returns, we will get the result of an address on on activity result. So there are there any questions? We don't really see any of that working, but we set the framework in place so that we can get it working the next time around. Are there any questions about, um, what we've looked at just in that just setting up the framework? Yeah, I have a couple questions. One is the on click function that you added in XML. Is that something that's just in 2.1? Is it also available in 1.6? Is it backwards compatible with older versions? Well, uh, yeah. It's available in 1.6. Um, yeah, but it should still run. I believe it. You know, it's a good question to see if because I mean, that's something that should be compiled or it it's not interpreting XML on the fly on the device. So I think that should run on older versions. That's a good That's a good, um, something to check into. But compiling it won't work on older versions of the SDK, that's for sure Won't understand that and sort of related to that. Is there a best practice to use one of the other the inner class versus setting it up in the XML? Um, yeah, I don't know. I mean, ah, my team has just chosen to do it that way because we're kind of familiar with it. And we there's less typing less. And also another thing this came have been forum conversations, um, creating an extra object by creating that anonymous, um, classes, you know, I mean, you're actually creating that object. So this way and creating objects is bad. It takes a memory, takes up time, it's, ah that you could there could be performance implications, them or objects that you create. Um, this way, this way that the sdk or the framework I mean may still be creating objects. But at least that's an optimization point that you can hand off to Android to do the right way. And if they have some better wayto to optimize things such that they don't have. Teoh, I don't know, go through the same steps that you would have to you. That's that's something that may be a benefit to. So I don't know. I don't know if there's I wouldn't call it a best practice, but I think that you wouldn't You wouldn't go wrong doing it this way. It's just that you have to be, um you know, cognisant of the fact that you're naming conventions is probably the best practice there. So you need to use good naming conventions. Everybody knows what components are gonna be calling. Which functions. Okay. All right. Um, And then one more quick question from my end, the on activity result method, that's it's a method and override method. Is that overriding the method in activity? Yes. So you can see that, um, and this is another good thing to call out, I guess. Thanks for reminding me if, Ah, if it doesn't happen to be the case that I know, um, that they're choosing an address and that everything is OK. Um then I want to make sure that I let the super on activity result have its have its say and the execution, so I don't want I don't just want to make this a black hole. And if it's not what I want and forget about it, I need to pass it up the chain, too. So everything works the way that the framework expects. All right? And Sarah Ruth, do you have any additional questions? Are we ready to move on? Is there a couple of brief questions about on Click? Someone asked. There's no need to use the class name and on quick clause in the XML, and I'm assuming that's answered by the internal class sources Class and XML Answer that you give and the other one is someone is not finding on Click available in 1.6. That doesn't seem right to me, but I thought I'd post that because I'm not on 1.6 right now. Interesting. I didn't think that was added in two point. Oh, maybe I'm wrong. Maybe. Was that in two point? Oh, um, if that's the case, then you'll have to just use the Thea. The other click. Listeners heading says that it is in 1.6 and he's got a link here she and pop that into the chat room for changes in Android 16 So might just be an error in that one person's set up and does. Does Android automatically do garbage collection on the anonymous object? One of those not needed? Yeah, garbage collection works, but garbage garbage collection is also a performance it So you know, you don't try to don't fill up the trash too much, because then there's a lot of trash to take out, you know. But again, I mean this SCK or the SDK, maybe creating code that does the same thing. It's just that you can let some smart engineer at Google fix your problems for you and make things a little more optimal. If if you do it this way by adding the on click instead, um, so whoever had the problem with the on Click not working on 1.6? Um, I'm not sure what actual problem they were having. Was it? Was it just that the Were they getting an air when they tried to do the on click, or were they not getting code completion? I'm not getting any response back to a couple of questions posted or to this conversation. So somebody posted the link already to the changes in the framework, and I think we can just move on from there. Okay. Having a momentary. Yeah, well, so my with with my cliff set up, so you know, that will happen. Um, you can take that to the forms. Yep. Before you go on to the next segment, we have one question from the audience, and then we'll go on. Okay, this on click feature. Um, can you Onley call to the activity or within, or is there some extended version to get anywhere else called another? Except when you go. But, uh, yeah, I think I think you would. Well, I don't know. I would be pretty. It would be, You know, maybe it might be nice to be able to call something on the application, which you could count on being there, but I would be scared to call something on another activity that would really couple this view to another activity that you know. Yeah, the modular approach would be best, and no matter no better if you could or couldn't, a modular approach would be best where, you know. Yeah, I'm basically what's weird here, and and you need to use the naming conventions and understand what's going on. But I'm tying. I'm kind of like specifying interface that any activity that uses this view needs to conform to, which is a little strange, cause s O. I mean, that's the weirdest part of this thing. But yeah, specifying an interface that other activities need to conform to. Or the fact that they need to be running would also be a little weird, even weirder. So I think that you can You can rest easy if you just make sure that this is a method that's on the activity of this using this for you.

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