Skip to main content

Add the Green Flashlight

Lesson 4 from: Developing Android Apps with Java

Tony Hillerson

Add the Green Flashlight

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

4. Add the Green Flashlight

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

Add the Green Flashlight

So if I want to open up the green flashlight activity, I want to come here and create one. So I'm going to go toe the flashlights. Ah, Package, Create a new class. I'm gonna call it green flashlight activity. I'm going to in the super class area. Say activity and I could hit control space in here and get some completion and shoes. Android activity. Now, when I say finish, that will be generated for me and it won't have any that boilerplate code. But that's fine. We can fix that now. I'm gonna go down to the layout and create a new file which will be called green dot xml instead of Maine. And I'm going Teoh shock and horror copy and paste everything from here from the main XML into the green XML and then just change a few things. So I wanted to be laid out basically the same except for the background. I want to be a green color instead of red for the label. I want that to be the green text which we've luckily already got in strings and I want the button on there to be labelled red, whic...

h we've also gotten strings and I want the I d to be red button. So hopefully everybody's keeping up with this. Um, now, when I say that there is one air because I don't have a green color defined So I'm going to copy this this guy and call this green and slip this, uh, ff from red into the green position. Now that should be fully green. And now, if I look at the green layout, I will have a green background. I could probably change the change, that label, but I'm not gonna mess with that right now. Now we have enough two inside the green flashlight activity. I'm going to use an eclipse shortcut, which is ah, under the source menu, override or implement methods. This is going to make it easy for me to add in a method that I want to override from the super class. And that happens to be a method um, called on create, which we've already seen inside the red activity. So I could come here and click this off in the override implement method dialogue and that will be generated for me, which is nice of eclipse. Saves me some typing, and I want to set the content view on this green flashlight activity to our dot layout, That green layout. And now, when I once I get to the point of showing this green activity when it gets created, it's going to show that that green layout that we just created now it actually get to that point inside the red flashlight activity. I am going to use an intent. So I'm going to define in a tent here, hit the control space that will, um, important. Just gonna call it intent. And I'm gonna create a new, intense, lovely job, a boilerplate way of doing things. Red tape. Um, it's OK, so the type of incentive we're gonna look at today, um, is constructed in the following format. First of all, I have to pass in a context which in activity is a context. Um, all you need to know is that it extends contact somewhere up the line, and I need to pass the name of the activity that are that I'm sorry, the class of the activity that I want to go to. So this is in a specific way of constructing an intent that navigates from one context to another intent. Um, because I am in this anonymous class to get access to the context Red flash activity, Um instance of this. I have to prefix it with the class name, which is just kind of another bit of Java boilerplate there. But then because I'm going from this context to the green flashlight activity, I give it the class of green flashlight activity, Dr. Class, And that tells it that I want this intent to navigate from the current context to this activity, and then to make that go, I just need to say, um, start activity, passing that intent, and now that should work. So let me compile that or and deploy that to our activity. I'm sorry. Are, uh are this guy? What's this guy called emulator? That's right. Somebody else. Such simulator apple, who calls this simulator and android causing an emulator. And I always interchange them, and I don't know why they're called different things. So, um, wait for it and we ready to collect the green button and click it. Oh, no. There's a hair. And I was expecting that. So let me show you the other thing that you need to keep in mind every time when you see this error or when you're creating an activity. Um, Android only knows about activities that you define inside of the manifest. So I'm gonna go open up the manifest. I do decided to so many times I wanted to make sure that we all see it inside of the application tag. We already have an activity for the red flashlight activity, but Android doesn't know anything about the green flashlight activity. So I'm gonna copy paste this guy down here, tell him over and make sure that android knows that we are expecting a green flashlight activity to be included are application. And this label is gonna be the title, by the way. So, uh, and it closed this guy. Um, so this label is gonna be the title. We'll just leave this to the APP name, which is just fine. Now, when I run this and the activity starts, press green and I get the green activity with no heirs. So one more thing, and we will be done with the code, and then we can ask questions. Um, I'm just gonna copy paste this guy too, because I want to do the same thing here. Basically, whenever I click the red button inside the green activity, I call this so if I save this file, it's gonna be confused about a few things. But that's fine. Ah, uh, cut this code out and here's another little clip secret You can, ah, click. I don't want it to be green button. I want to be red button so I can click this and say, right click and se ri factor. And you could see this shortcut keys for your particular operating system here. I'm going to say we factor rename. And now when I type in red Button, it will very helpfully change all of the instances of that variable to the into the name that I wanted to be, which is good. And this is no longer gonna point out. Green Button is gonna point Red button. And now we have a click listener. Let's define that's gonna handle to click on the green screen. But in this case, we don't want to fire an intent in at least for our particular case, we want to instead finish the current intent. So we called a Finnish method, which is on the activity and that will that will stop this intent. Just like think of it like a deck of cards. I haven't intent that opens up from one activity to another. That activity is laying that activities. View is sort of sitting on top of this, this theoretical stack of activities and when I finished, this activity will go back to the last one in this stack, just sort of like your browser history. There's also more complex things you can do there. But for right now, for our purposes, we're just gonna finish this activity, which will go back to the red activity. So if I run this fella and our emulator, so about this emulator to there's another important point that I could make an emulator is not a device you always wanted to test. I mean, it's very convenient to have everything in one place. Some people have noted that it's slower, and this is really taking a long time. I don't know if this is gonna make a device not found. That's great. I think it lost it. Lost the connection to the emulator. Sometimes this happens. That's really irritating. Basically, I have to close. Did someone to. I think someone might have said this in the background. If I closed, the emulator started again. It's gonna launch time to anyway. Let's go. Great can suggest that you run a clean on that clean. Maybe I don't know if that will make it connect up with the emulator, though, because it's it's connecting over local socket. Um, As you can see, I started of an older version, the emulator here, which should still run Arco just fine. Any All this starts, keep in mind emulators, not a device. You owe it to yourself and your users to test your application on an actual device before you deploy it to the store every time. You didn't believe store cause there are. There may be subtle differences, and I can't tell you what those are right now because there's not supposed to be differences. Um, but there may be subtle differences in the execution. You just need to make sure that that's not the case. At any case, even if there's not some weirdness in the execution, you need to feel what it's like to use it on a device as well. Because you're emulator. You can do things like limit the band with speed, so simulate a edge connection instead of a three G. But you can't simulate how slow it runs, and it's gonna run with the full power of your local processor. So you need to feel what your device is gonna be like. Chuck, what this experience for users going to like chugging on on a device and not on your desk, your desktop and little things like, you know, tapping around your finger as opposed to a mouse. Okay, well, let's just take this for granted. Well, it starts up. That's gonna work because my code always works right and go to questions. Hi, Can you post again? The codes that people can take a look at the code while we're answering questions in the background? Sure, they are. My well, this is a least the green activity. Just taking a really long time. It's it's It takes a long time to boot up. Okay, there's no way we had a couple off a couple of questions from people about the r dot I d. And when that's created, um, I'm not sure what you mean by created, but the it's created the first time, I I guess if you mean by created like it becomes something that ah android takes Noto notice of inputs into the our file for you to use it gets created whenever your product is built. And I guess I guess there's an implicit assumption here that, um, what I'm doing in eclipse is have this under project I have billed automatically checked. So every time I change a resource and save it, Android the android I d thing rebuilds my project, and it will pick up any changes to re sources and change that our file Every time I save, um, you can turn that off if your build is taking a long time. I work for a company called Bright Kate for a service called Break Height, which is sort of like a Twitter with location. And we had an android app, and that thing was huge. There's a lot of stuff in there, and so the bills were kind of long. Sometimes when I was on a machine with some low resource is, I would often turn off that build automatically and sometimes forget, you know, when adding any resource why it wasn't working to use it like reference it inside Java code. I needed to just go build project. So that's that's when it happens when when it builds. Okay, The emulator stood up. There we go now clicking red goes back to I mean, it looks like it goes back to the red activity, but what it really does is finish the green activity, which just pops it off the stack there and goes back to our red activity. So questions, other questions. So you mentioned the at plus, uh, could be used anywhere in your file. What would be the best? The best place to put it is where you need it to be defined. Um, like for this, I want this to be for our Teoh uniquely identify this particular button online 14 or 15 of the green XML um code. So I wanted to find a pair because that's what it references. There are only weird reasons that you would need to define it somewhere else or do the first reference somewhere else. When we get into some of the layout complexities in later modules and you'll see the only reason I mentioned it there. In fact, I shouldn't shouldn't have even mentioned it. Probably too confusing at this point, but we'll get into the reason why you wanted to find out somewhere else in some later modules. We've had a couple of requests to go over creating the new activity again. Okay, so I create that just like any other job class through, um, by right. Clicking on is one way to do it on your project and go down to new class because it's just a Java class. The only thing that made it a activity is that I extended in this super class area activity. So this would make a new activity in the default package because I clicked on the project. So the package. But anyway, this this is the only this is what made it activity. Does that make sense? So, I mean, my package structure here is just calm down, O Reilly down Android. A flashlight. But if in a real application, I would, you know, have a better package, lay out with something underneath, like probably keeping activities separate or either I want oh, you know, separate him out. Just like any other job of project. By keeping activities and package or maybe doing it more like functionally. So I want to keep everything that has to do with the flashlight, part of my app under flashlight and everything that has to do with my contacts management part of the app because this is gonna be a flashlight app with context contacts in it in a In a calm, down around O'Reilly Don Android eye contact or something. I don't know eso you can. You can organize things a lot better, but there's nothing special about him there just job classes. Does that design answer the question? Okay, so that we have any other doing any questions from the audience? I think one person wanted to ask a question just to understand that use have finished their If I had done on Click and gone through what I did over on the other button on Click, I would actually have one activity on top of another activity on top of another activity. Is that right? Good questions. So I don't actually want 1/3 or second version of the original main screen. I actually want to go back to the original. That's a good That's a great question. Yes, yes, that's exactly the case, you would have another instance of the red flashlight activity. So there's a way to manage that. Um, you can This is getting a little bit more advanced. I don't even know if we cover this in later, later modules. But you can set Ah, setting in the manifest about that activity saying that actually, you could define it when you when you set up the intent to I can't I can't type it right off the tip of my fingers here. But basically, you're saying, um, I want you to start up this activities. Let's say it's called red flashlight activity. And if you find it earlier in this stack, pop back to that location. So, Tony, we are out of time. So, um, I want to say thank you to everybody who came and thanks to our online audience, and I know there was a ton of questions that we didn't get to, so we are gonna have a forum set up, and we'll have more time for questions next time as well. So we have five classes. There's five more of these classes where we're gonna learn some or interesting stuff than just making sort of a toy flashlight application, and we're going to building a task manager application in later. Um um, later Ah, modules of this class. We're going to start out by just in the next week next Tuesday, building a very simple form to enter a list of tasks into our task manager application and then just sort of displaying those is a simple bulleted list. So then you'll get an idea of how to take in user user input through other means than just clicking, and we'll have some interesting stuff to show their. And then we will continue to build out that application and use a list component, which is, Ah, a much better way of showing lists of data. And we'll be able to then, like checkoff, different tasks in our task manager in the list there. So those are all nice, more interesting things to do with the interface. We're going to learn about some more components and layouts, but then we're going to stop in module three and re factor what we've built so far so that everything is all the tasks that we've entered into our application are no longer stored in memory. They're gonna be stored inside of a local database, which is good for ah when the phone has to be rebooted or go through a update to the application and things like that, and we'll learn how I learn how to deal with the local database when that's in Weeks Week Number four Accounting. This is week number one and then we're gonna get into the really interesting stuff, which is looks a lot better and is more satisfying to build. We're gonna build a ah, a way for the tasks that were keeping track of in our task Manager Teoh have a location associated with them. So we'll build a way for you to look up a location by address in the phone, map it on the, uh on on an included map and then and then associate that location with a specific task. And then we'll continue on with that location theme in the final week of this set and, um, get the location of the device so that we can match that with location of any of the tasks that were storing and see if maybe there isn't something that on our task list that's that's close by where we are right now that we want toe check off, and that will get the device that will look into the location. FBI's getting a GPS or WiFi location of the device and also using that Teoh programmatically um, compare against and location that's stored on a task. So those are some of the more exciting things that we have, um, in store for you over the next coming weeks.

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