Skip to main content

Create the Add Task View

Lesson 8 from: Developing Android Apps with Java

Tony Hillerson

Create the Add Task View

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

8. Create the Add Task View

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

Create the Add Task View

all right, let's start adding tasks. So the next step is we need to add tasks through our ad tasks view screen. So we need to set up the view to let the user accomplish that. So what we're gonna let the user do is put in a name for the task, and then they can choose to cancel out, which was just would just back them back to the to the last screen. Um, and of course, they could use the back button to do that, too. Then, um, when they click the add task button, it will add a task to our shared list of tasks in the application and then cancelling will simply return the view screen. Like I said earlier, So basically what we need to do is set up an input text type of a field and then two buttons on that ad test screen that we already set up. And then we need to add a click listener for ad task, and we need to, um, set up the edit text to ah, have an I D. So that we can get reference to it and grab the the text to get the name for the task. So let's do all of that right now and that will tak...

e place close and files here inside of our if you layout, add tasks, which is right now just a simple linear layout with a vertical orientation. Um, this is all stuff that we've kind of seen before. There's not too much that's different here, except you're gonna run into a new component you haven't seen. But I'm going to copy and paste that in just to save typing again. So we have a text view, which is the title at the top, and that is going Teoh reference that same string that we had on the button. Just add task that will be the title, um, on the screen. And then there's an edit text, which is a new component, which is a ah text area that a user can type into in 97 I d. And it does. We'll call that task name, and then there's an ad button and a cancel button, and I think that we will need to once I save, we'll see all of the errors, and I think that this will be the only one we need to create a string for that, That cancel button. Someone opened the string, file my open resource shortcut and duplicate this line because it's about the same writing and at a cancel strength. So now if I close that strings file and come back to add tasks, then there should be no errors there, and this view should be ready to go. So the next step is to go over to the activity that controls this view. And that is the ad tasks activity view or I'm sorry, attacks activity. And we need to do the same sort of song dance here we need to set up use. I'm gonna create that private little helper method to sort of consolidate all that view. Um, set up code at task activity. I'm going to copy in some of that stuff, save some piping again. So I want to get reference to three different things on that view that we could use in code. I want that to be the edit text, of course. And then the two buttons. So I need to import those guys for button task name at button Cancel button, and then I will set these up his fields task name, edit text and edit text component the add button and the cancel button, which are buttons. Okay, you'll notice that eclipse to If anybody's wondering what this yellow underline means. Red underlines mean a problem and yellow underlines mean a warning. This all comes from compiling the Java code. In this case, it's saying, Hey, you're writing to this variable butcher never reading it. So that's probably not a variable that you need. Are you sure you want to keep it around? And in our case, we do just that during the code creation will have that that show up sometimes. Um, OK, we've seen lots of U V listeners Click listener. Sorry. So hopefully don't mind me pasting a little bit more code and not really going over it too much. But we need a on click listener for both the add button and the cancel button. So I'm gonna copy those guys in here, and then I'm gonna call a method called at Task in the in the case of the add button and cancel in the case of the the cancel button hopes one problem here. So another thing that needs to be the case when I hover over here. Um, can everybody read this? So? So the text in this Ah, this pop up says the method cancel is undefined for the type new view on click listener. There, too quick fixes available. You can create a method. Cancel or you can create a protective method canceling type add tax, add task activity. Of course we need to do that if we're to share Resource is because again, we're inside of an anonymous class here. So I'm gonna quick add these guys and I'm going to move these up, cut this code just just for a little housekeeping here. I think these are a little more important than set of views. So I'm gonna have these at the top. So given that somehow we want to add tasks or cancel these methods will be called and we could do some work inside of there. Okay, Um, for now, I think that what we want to do is let's do cancel first. So does anybody have any idea what we would do in the case of a cancel? We could do something pretty simple here for now. What would we do if we want to cancel out of adding this task. So would we. We would just end the view toe, add the task. Yeah, we can just do that by using the finish method. Like we used last time to go from the green flashlight activities, the red flashlight activity. So that would just finish this This activity and now at task. What we want to do is actually at a task. Is so, um, to do that, we need to do a few things. First of all, we need to get the text that the user typed in. I'm gonna set up a local variable here which references this task name, edit text at a text component and then to get the text out of there, we have to say, get text was injured, being a new instance of edit herbal and then calling to string on that will give us the actual string that the user typed in. So now I have this string typed in that they want for their name. The next thing I need to do is to create a task using that string. By the way, I could I could abstract all this into the it probably better to put this inside the application, but I wanted to be very explicit about what's going on here. So I have everything set up right here. Um, covering over this task. I need to import that. And now I have a new task. Now, I want to do something. Um, that we will come back to later. Um, and there's a reason I'm going to do this and I'll explain it to you as I paste it in. So I wanted I need to get a reference to the to the application that this activity is running under, um and he will come see how I could do that in a generic sense in a little bit. But I want to do this specifically to get our our task manager application. Um, because that's the one that has add task on it. So I'm gonna create a little helper method here, create method, get task manager application and the object. I'm gonna change that. That generated code to return a task manager application instead of an object. And the way I'm going to do that is like this task manager application equals get application. So here is a I help her method on activity called get application. What is it? Well, we'll get us the problem for it. So there's a helper method on every activity. This is of type activity called get application, which will get the application that the activity is running under on that will return. Obviously a type of android, that app, that application instead. So I need to cast this to what we want, because we know that's what it's gonna be on return that guy. So get application is on our every activity returns the major returns, the activity that absolutely application that the activity is running under. We have our own application. So we're gonna return that one instead. And then we'll come back to this this helper method in a little bit and push it up one level so that other activities can use it to. But for now, we're just gonna get this, um, get a reference to the application and at a task through there and then once that work is done, that's good enough. Rush for our work so far, and we can finish as well. So, Tony, just to just to sum up what you just said, basically you're saying that we want to call the ad test method on the application, Um, in order to actually add the task, because all the task is ah, the tests are shared across the application at the activity. I'm sorry at the application level, and Ah, so we need to add the tasks through that interface that we've created for ourselves. And then the view tests activity will have access to that through the same month. It's the same way. Okay, I'm going to do that next, actually, because, um, what we need to do now that we've added it's asked to prove that to ourselves and to make the application were great. We need to actually get the tasks um, displayed on the view tasks activity. Hey, Tony, Interrupt. I have a quick question here from the chat room. It's. Would it be more efficient to have the buttons seized? The activity is the click listener having the activity implement on click listener and you say a switch in the on click method or to do it the way you have done it. Um, that's up to you. I don't like. Okay, so they're what they're asking is basically we can set up our activity as a non click listener and then listen for all clicks of all buttons and then decide which button was clicked. Hopefully, that makes sense to everybody. Um, I just don't like doing that conditional code. It seems messy to me, but so I mean, in terms of typing and may actually be a little less typing. I like to set up, um, Anonymous click listeners. That's just my preference. I don't I don't know if there's really any, um, you know, efficiency gains either in the code or in the typing side of things. I just like, I think it think it's cleaner code to my from my taste. So that's kind of a matter of taste thing that you have to sort out on your team if you work there or do things whichever way you want. If you want a could you do me a favour? Whoever asked that question and ah, start up a topic on the forums about this, that we can talk about a little bit more cause that'd be something useful to talk about. Okay, so now, um, back to the view tasks activity now that we have something in our task list. What we want to do is show those tasks on the view. So I'm gonna paste in a method that will do that right here. And that is called show tasks. So basically, the way I'm going to do that, I mean, I'm just gonna do all the work here, and this may seem kind of messy to some people, but don't worry. We're gonna move this out in later episodes. Um, but basically, I'm gonna get all the tasks that we have in the application, important these guys and then loop over them and, ah, just create a little string rougher here and then push their push their names, which we could get through to string into the string buffer. And then hopefully everybody understands this Java in can't ation. Here, this is just gonna create a bulleted list with a string followed by a new line. And then the string will be the tasks to string, and that will be upended into the string buffer. And then once we have all the tasks in there, we can set the text of the task, um, text view to be the contents of that string buffer. So you can see here that in, like, pasted code I expect to use a method called get task manager application. And you may recognize that if I switch back to add task activity to be just this so what I'm going to do, I'm gonna show you a little eclipse trick, which helps re factoring these type of things because now we want, um, just as all android applications are I'm sorry, activities, all android activities have a get application method. Ah, we want the activities for our application to have a get task manager application methods so that they can get that in the same way and get the class that they expect instead of the generic get application. So I'm going to do is cut this code. Actually, no, I'm not gonna do that way. I'm sure you how eclipse will do it. So I'm sorry I'm back here in the ad task activity, and what I'm going to do is hover over this this method and I'm going to go to the source. No going to go to the re factor. Um, but what I want to do is pull up this method to a super class. So what's gonna happen is I'm gonna create another class that's a super class of this activity adds, ask activity and we'll call a task manager activity. And then I will go to view test activity and make it extend that same class And to help me, um, do that, I'm gonna have go through this process that eclipse will allow us to do, which makes this all kind of easy. I'm gonna pull this method up to a super class pope is not available in this type. All super types are binary. Okay, so first, I need to actually create the apple, create the activity. So I'm gonna come here to this task manager package and say New class task manager activity. So this is gonna be a generic activity that I can subclass and get access to some shared code. This is going to extend activity going to finish that, and it will create the activity and show me the code. And this is gonna be a simple guy. Um, I thought I'm sorry. I thought that eclipse did this part for me. I think that it may in a different context, but not in this. Apparently, I didn't do it correctly. But given the fact that if you test activity instead of extending activity extends task manager activity and add task activity extends task manager activity. Then I could go back to view task activity and try this again. Have some. Sorry, a task activity. Try the skin, come here to the re factor menu and go to pull up. Now, it will give me this dialogue where I can choose to pull this method up into a A, uh um, super class and the destination type will be here. Comma. Riley Android, Task manager, Task manager. Activity finish. And now eclipses help me re factor that. So now get task manager activity. I'm sorry. Application. This method will still work inside act task activity. But inside view tasks activity. I can now also call it, and it will work. So I just did a little bit of re factoring there to give us a a common super class with a task manager application. Um, notion of the application, the instead of your season, get application. Any questions about that? Yeah, we had a few questions. Eso I just wanted to kind of summarize and see if I got it right. And then if I got it wrong, you can correct me. So basically we wanted to. We need to be able to add a task, and we need to have access to the application to add the test so that it's available to all of the activities. So you wrote a little helper method. But rather than have the helper method repeated in both activities, you pulled it up into a new super class of activities called task manager activity, so that all of the activities would have access to the help of method. Yep, that's exactly right. And so, in this sort of view of things that we had got the two different activities here the view and add. And then there was an application, and above them now even started a activity that both that's a common ancestor of both of those activities and that had knows how to access the application so that Teoh each activity could share that code. That's just a little bit of a timesaver. I kind of I kind of went through that exercise so that I could show you, um ah, eclipse her factoring Sorry, forgot how that I need to take one step before the other. But that's how they're factoring works. And that's probably useful to you at sometime in the future. So now, um, by the way, we can clean this up because we no longer have to import activity. So that's why this little yellow swig squiggly is there and add task activity and in view task activity. I could get rid of that code there. Now, we should have a, uh, a situation here where, in view task activity, we can call this method show tasks, and it will get a list of all the tasks that the task manager application knows about and get all that text together to show. Um What? What's the right time to call that? Um, when we're coming back to this view from the ad task activity, we're gonna need to refresh that. And so, just as ah, what's what's called an activity lifecycle method of on create is called when an activity is first created. There's another life cycle method that I'm going to introduce you to right now called on resume. And I'm just pasted in some code the name of the method is on resume that comes from activity, and it will be called whenever an activity comes into view again. And it will also be called during the create life cycle. So when the first time a activity comes into view, firstly on create will be called and then later on resume will be called. And then after that, every time the activity activities view is shown again already the activity comes to the forefront. Ah, on resume will be called every time. So every time on resume is called, we're gonna call show tasks and it will go show all this ask that are so far held in memory inside the application. So I'm gonna run that and we should see everything working as expected. No sneaky ah, errors like I had last time to show you about the manifest. Um, I see. So if I click, adds asks, and I could come here and say milk add that task. And now it should show up inside the task manager. So I think that covers it for this step. Any more questions about that one in particular? Some somebody asks about the persistence of tests across the app and um, we are gonna get to that in a later session when you cover databases, right? Yep. So right now, this task list is stored in memory. If you Ah, if you move to another application, your your task manager affiliation will probably still be running unless you run into a low memory situation or if you go so it. So in that case, the task list will stay around. But if you reboot your phone or you kill the application, the task manager application, those will be gone. So that's not the most useful thing possible. But there's a couple different ways that you could save those tasks locally. Um, and we will look at that in the near and and the in the near future and in a later module.

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