 Hey everybody, this is Brian. Today we're going to do our fifth Android tutorial and we're going to do it a little bit differently. We're going to take all the knowledge we've learned thus far and make a simple program and we're just going to click start and then we're going to say let's call this app five, next, next and then it's going to leave it as main activity and we're just going to hit finish and then Android Studio is going to do the trajectory of everything in outer space because it takes forever sometimes. All right, so we have just our bare basic program and if we look at our resources we just have one simple layout. We're going to get rid of that guy and we're going to add a couple things here. First thing we're going to do is add a new Java class. We're going to call this app constants. Why app constants? I don't know just because I needed a name. Basically in this class we're going to put all the things that we're going to use globally. We could call it app globals or whatever. I'm going to call it app constants because I want to and we are going to add a new maybe layout. There we go. Or not layout, sorry, activity. We're going to add an empty activity and we're going to call this settings. Finish. So now you should see your settings XML here. So now we've got our two activities. So the first thing we want to do is jump into this manifest and actually add this filter here. Otherwise we're going to forget and it's going to really make us mad later. And we're going to say and the rule here is a default. That way we can actually just open that bad boy. And what I like to do is just to make sure it's not going goofy on me. We just give it a good build here. So we're going to rebuild, we're going to watch Gradle build the project down here. So what we're going to end up doing here is we're going to make an age calculator. So you've heard like the term in dog years, what we're going to make like in dog and cat in fish because why not. All right. So what we want to do here is we want to take a text view. And let's call the, let's actually do this here here. I'm trying to multitask and I've had a long day like crazy long day. And we're going to take a, where is it a number? Text input field. Put that here. Fine. Just do it the way you want to Android Studio. I don't care. Just put it up there. All right. So we're going to call this txt age. And then we're going to grab a button and we're going to call this, let's not call it, say it's called calc. Let's say my music stop for a second there. I like to listen to music when I write code. That's why I was like, what the heck? All right. Then we're going to make another button here. And yeah, I know Android. Let's call this btn settings. So now we've got calculating settings. And the premise here is that we're going to type an age we hit calculate. It'll calculate it down here. And let's put that there. Let's call this LBL result. We're going to just make that big and make it really big. Let's make it like crazy big. So the premise here is we're going to type an age at calculate and it'll convert it into whatever animal age that we want. So now we need to look at our settings. And from this, I should say from the last one, we did a radio group. Remember, we did the how to work with radio buttons. We're going to do that same thing. We're just going to drag and drop some radio buttons down into here. And then we're going to just radio dog. And you guess that we're going to call this dog. We're going to make dog check by default. And we'll make this radio fish. We could make a bunch, but we're just going to stick with three animals. So we could do cat ears or dog ears, cat ears or fish ears. I don't don't really know if there is such a thing as fish ears, but whatever. We'll just figure it out. And then of course we need our save button. Come on Android studio, don't mess with me. There we go. All right, so now we've got our layout done. So when we're in the main, if we hit settings, the settings activity will open and we'll be able to choose what sort of animal we want. We hit save, it'll let the main activity know how we're calculating this. So now it's time to make all of that magic work. First thing we're going to do is jump into app constants here. And we're going to say public static, if I could spell public, like I said, it's been a long day. We're going to use our tag for our debugging. You can use whatever you want. I just use my website name. Maybe. And now we're going to make any num. And this might be a little little cumbersome, but it serves a purpose. It's actually pivotal to the application. I think without this, you know, the fate of the free world would just cease to exist. I'm not a fan of the way Java handles the nums, but some people like it. All right, so we're going to say none. Let's see here, dog. So I went out and I actually legitimately researched this. And the consensus on the internet is that every human year is equal to nine dog years. I'm sure the internet is just going to explode and say, that's not how any of this works. And cat ears, I actually googled that. And it was six years, because cats don't typically live as long as dogs, which I think is not sure I've had cats outlive dogs. Fish, we're just going to say three, but I'm sure there's fish out there that live like forever. All right, now we're going to add a little bit here. I'm going to say count type. And I forgot something. I made a boo boo here. Yeah, if anybody's having these screen tearing, just message me on Facebook and I'll give you the actual code. It was an Nvidia option you got to go into and save to an xorg file. All right, so that's ID equal I. So what we're really doing here is we are building our enum and enum in Java is actually really a class everything everything in Java is a class at some point. And we're going to just add some logic to help convert some values back and forth. All right, so public int numeric type. So we're just adding in that function that'll say, hey, we want this as an integer. But now we want to be able to convert from an integer back into this calc type. So we're gonna say, oh man, I had something in my eye that like stung for a second there. Don't you hate that? And especially when you're like driving or typing or something, you just can't stop. We're just going to go through these values here. And we're going to say if, you know, type, get numeric type. So here we're calling the previous function get numeric type. We're saying if this is equal to the value, then we are going to return type. Whoops. Otherwise, we're going to return null. And we could be fancy and actually return none, but I'm kind of lazy and I just want to return null. Oh, what is this thing's problem? What is it complaining about now? Make faraday from void. What? What? Oh, I see what I'm doing wrong here. You know, it's one of those nights where you just sit there and get an argument with the compiler. And let's give this a good build just because I'm tired and I don't really trust my typing. A couple of people messaged me and said, dude, your videos, you sound really just worn out. I moved into an apartment. I'm still not used to it. So I'm not sleeping well. All right. So in short, we have our tag, which we'll use for logging. And then we have this enum type called calc type, which will determine the number of years we're going to calculate on. And then we've got some functions in here that basically tell us, you know, we want to get it based off an int or we want to get the actual calc type numerator. So we're just going to ignore that for now. And we're going to go in here. We're in our main activity. Ow, I hit my funny bone. And that was not funny. Ouch. All right. So, um, whoops, whoops, not text edit. What am I thinking? Edit text. Geez. Let me move the mouse out of the way so you guys can see that. I really hope I got that screen flicker taken care of. And then we're going to get our buttons. This is probably the boring part where you fill in all the code. But, uh, all right, now we need to actually get all those guys. So we're going to say dc age equal r dot. And right now, this is probably boring for you guys because you've seen this before, but we're getting there. Trust me. And then we're going to say, whoops, how many results equal text view, maybe text view. Really? There we go. All right. Now we're going to grab our buttons and wire those up. So we're going to say btn calc equal button, magically copy and paste this. We've got our setting there. Grab this little guy and we're going to make our on click handler here. Actually, before we do that, let's make the actual functions. Some people like doing it in line. I like making it an actual separate function, just for readability, I guess. If you're sitting in a classroom, some instructors will tell you don't do it like the way he does it, do it the way the book says, the way I said, or really there's really no right way or wrong way of doing code as long as it actually works. That's the beauty of programming is you can do it a million different ways. I had a teacher that was very, very, very strict on how you did things. And when I got out of the real world, the way he told us to do things made absolutely no sense. And to be blunt, the company I worked for just hated the way he did things. So it was a very much of a learning curve to try to reprogram my brain. All right. So now our buttons are wired up. And let's just do a little bit of logging here so we know what's going on internally. We'll see. Calculator. See, calculator. That was a joke. Anyways, that's a bad joke. And we're just gonna, in case you did not get that joke, like, what is he on? It's an accounting joke. I'll calculator, get it. Anyways. All right, no more jokes. All right. So we've got our buttons wired up, and then we need to be able to actually do some things with these. Let's actually go back up here. And let's make another variable. And I actually do want to give that a default. And I'm going to leave it default as cat ears because I like cats, but you can do dog or whatever. And I think I accidentally deleted something here. I did, didn't I? Let's give that a rebuild just in case. Make sure it didn't screw something up really bad. All right. So now that we have this variable here, this calc underscore type, which uses the enumerator in our app constants, we're going to use that to determine how the calculation should work. So we're going to actually work on our calculation code real quick here. We're going to say int age equal parse int and we're saying txt age dot get text dot two string. So we're going to get this the string out of that txt age, that top numeric box that we put in there. And we'll say int results equal. And this is where we're going to actually calculate here. We're going to say age time calc type dot get numeric type. So what is happening here? We are getting the age out of the actual user interface. And then we're saying age times the and we're taking this enumerator get calc get numeric type. So this is cat and we're going to say get numeric type. So let's jump in here and it's going to say cat is six. So get numeric type is going to return this dot ID, which is this number right here. So that's all we're really doing. When the enumerator is created, we're saying what type is it? We're saying cat. So it's going to show the cat in here is going to say this is six. Not exactly how that works under the hood, but you get the picture. So when we call get numeric type, it's going to say this dot ID, which in this case would be six. If it was dog, it would turn nine, fish would return three. So that's essentially how we're calculating this. So let's actually run this app and see if we're at least calculating something. We're going to start up our virtual machine here, or I'm sorry, our virtual phone. It's booting up. I'm actually going to set up my logging here. Yeah, because it's already spewing a bunch of stuff here. Get that going here. Oh, I think my emulator died. There it is. One of those instances where I broke Android, I'm known for breaking things, especially when I'm recording. It's not fun. All right. So here's our multi-billion dollar application as soon as the emulator actually loads it. There we go. As you can see, we're going to get just filthy rich off of this. So our age is zero. So we're going to type in an age, hit calculate, and it should multiply it by cat. So we're going to say four and we're going to calculate. And nothing happened here. Hmm. You know, I think I know what the problem is. I think we didn't actually update the GUI. I could be wrong, but I think the GUI is not updating. Ow, gosh, I hit my funny bone again. That's just ridiculous. All right, now this should actually work. Let's run this bad boy. All right, let's try that again. So we're going to say four and we're going to hit calculate. And now it's 24. So four times, or I'm sorry, 24 divided by four. You guessed it. Should be that. Bam. Let's actually kill this. We want to build the settings now. Now to do the settings, we want to actually launch the activity, let the user choose, and then get the results back from what they chose. Now, if you remember correctly, I said in that tutorial, do not do this, but this is one of the instances where I'm going to do it. The reason being is the information is not going to be persisted. We really don't care if we lose the information. It's just a simple, hey, what are you trying to do? So to start, maybe we're going to do a control. Oh, and we're going to say on activity result, just so we know what's going on here. And let's actually start our activity. So we're going to say intent, intent equal new intent. Wow, that's a mouthful. And we want to go into our Android XML and we want to actually get that path here. And we're going to say, whoops, start activity. I failed twice. How do you fail twice in a row? We're going to say, whoops, start activity for result. We're going to give it the intent, and we're just going to give it a zero because we only have one that we're starting, so we really don't care. Remember, this is the request code. Because we're only requesting one, we really just don't care. So this will start the settings activity, and then we'll get the results. Let's test that out real quick just to make sure that portion's working. Flip back in here. At five has stopped. Why did at five stop? That's not good. Why did that stop? Well, this is always interesting. All right, it's debugging time, ladies and gentlemen. Let's figure this out. Wait for the debugger to attach. Let's see what I did wrong here. It's probably fairly obvious because I'm tired. We're going to clear that. Go back in here, hit settings, and what exploded. No activity to found, and no activity found to handle intent. The setting. Oh, they're missing the S at the very end there. See, I misspelled it. It's settings, and in here I'm calling setting. There we go. That should work. Wow, that's embarrassing. All right, let's run this again. Launch this bad boy, and now our setting should show. Oh, yeah, I have another application in there. That's interesting. I actually wrote this application on this phone. I forgot about that. All right, so anyways, we can choose here. Let's hit that again. Now, in case you're wondering what this is, this little thing, see that at five pre, I actually write the program in completion, then I take all the code and I put it into notes that are off the screen, and what's going on here, and this is really important to understand, is because we're calling that, it has the same namespace. So when you look at this, that is the exact same. So if we change this, so let's call this setting two, and then we call this setting two, which I don't even know if this is going to work, but we're going to try it. Now, when we click, it just opens in our application. The reason being is you have two activities that have the exact same name in two different applications. So let's actually hunt and pack through here. Yeah, you can see there's pre right here. So this is the other activity, and wow, there's a lot of programs that I put on this guy. So let's just kill these things, and let's go back into our manifest. Let's delete that too, and let's see what happens if we do this. I'm actually kind of curious to know what happens. So the application is not running, and now we'll hit settings, and you see how it's still asking. So the point I'm making here is your activities can be a central part. So if I do using pre, it'll actually open the pre. You see how it's at five pre. Remember, pre is the application that I wrote two hours ago, not the application we're working on now. So that's important to understand, and that actually I completely forgot about that to be honest. So to avoid that conflict, we're going to say two, just because I already have it in here. That way we know we're launching it in our application. Now in reality, you should put like my app, blah, blah, blah, something, something, fully unique name. All right, one more time just to make sure we're doing this correctly. All right, we're going to pull up our monitor here, and we're going to click save. There you go. So you can see we're pulling up settings right here. Those are all our little actions. So now we're going to actually code this in a little bit. And we're going to say int value equal data, oops, dot, get int extra. And we're going to say type. Remember, with an intent, you can put extra data. So we're getting the extra data out of here, which we haven't added yet. That's why you might be a little confused. It's going to be in the actual settings activity that where we're going to do this. And we're going to say our calc type variable is going to be app constants, calc type from ID, this is where we get that. So we're just converting the integer that we're going to get out of the intent back into an enumerated type, and then say, hey, our enumerated value is going to be that. And then we just want to say log dot d, app constants tag, and we want to say calc type dot to string, because we just want to know what value we're returning from the settings. All right, so now our settings should be pretty straightforward. You should note that if we try to run this now, we will probably get an error and it'll crash because data will be null. Just to prove that, let's just run it and show you that it will crash. So if I hit save, it should go boom. Oh, actually, I'm sorry, nothing happens because I haven't done it. Yeah, now it crashes. All right, so let's wire up that settings class. And we're going to say private button, btnsave. I've been getting a lot of email while I'm typing here. A little bunch of people are going, hey, dude, do you actually program? Where do you work? And well, I'm actually not a programmer anymore. I'm an IT manager. Why money and time? When you get to a certain age, you really just don't want to compete with people that half your age, willing to work twice as many hours and willing to work for half the pay. It's just easier to be the manager of those people. So if you're wondering, do I regret my decision? No, I make really good money and I actually enjoy teaching people, which is why I do these tutorials. But my job allows me to actually teach people, which makes my job very fulfilling. All right, so we got our button. Now we're going to get the radio group. And this is something we haven't done before. We're going to say radio group. And I completely forgot we need to go back in here. And we need to actually click on the radio group. And notice how there's no ID, which means we have no way of grabbing this thing. So we need to actually give this an ID here. So we'll call this radio group options. That way we can actually get that. Otherwise, the Android Studio will not make an actual variable for it. And there's no way that we can actually use the fine view by ID. We'll just get an error message because there's just nothing there. So we're going to say radio group options. Oops, did it not take it? Now I'm kind of curious. It didn't. I'm just going to click off and then click back on. Yeah, it did take it that time. Maybe it's something I screwed up. There we go. All right, so we've got our, why is that doing this? Required radio button group. Well, that's why I've got my value or my variable types screwed up here. There we go. Thank you, IntelliSense. Man, I remember the days when you used to write code and you didn't have that and you would sit there and you'd have to compile for 20 minutes and then figure out what the heck blew up the good old days. So a friend of mine, we were having a really in-depth conversation about life and stuff like that. And admittedly this was after a few drinks. And he said, dude, you've got these videos. Why don't you charge money for these? And I'm like, I don't know. I just, I feel weird putting advertisements on it. It's just not really what I wanted to do. I mean, it doesn't cost you guys anything if there's ads on it. It's just it annoys the hell out of me. All right, so what we're going to do now is we're going to get the original intent. So this activity was launched with an intent. If we flip back into the main activity here, you'll see how we do start activity for result and we're using the intent. So we're getting that intent. And now we're going to actually work with that bad boy. So we're going to say selected ID. And we're going to say radio button group. And we're going to get checkbox, checked radio button ID. That's a mouthful. So we're getting the selected ID. Now this is not the number of the numerator. This is the actual variable, I should say the value of that control in memory. It's a little confusing, but remember, everything basically has an ID inside of Android. So we're getting that ID of the checked option inside the radio group for a visual representation of that. If we choose cat, it'll have the ID of cat or fish or dog or whatever we choose. So that's really what we're doing. So instead of doing some monster if then statement, we're just saying which one of these is actually checked. All right. Now we're going to say radio button. And we're going to say selected. And this is going to be a little bit of code, but we can do it. Oops, maybe radio button. Oops, radio button group done. Now notice how we're doing radio button group find view by ID. We're not doing the current activity or the current context. The reason for that is we want to find that ID from within the radio button group. So we're going to say the selected ID. If you try to just do a find by ID, you know, this dot find by ID, you're just not going to get it. It's going to error out on you. All right. Now we need to do we're going to get the actual text value of that control. And then we're going to say int value, they call have constants, count type. And we're going to say value of and we're going to give it the name. And then we're going to say get numeric type. So in a short version, what we're doing here is we're getting the actual name of the radio button that's selected. We're taking that, converting it to the value and then getting the number type of that value. So if it was a dog, it would be, what did we say nine? Cat is six, fish is three. And then we just want to do a couple of log statements here. In case something explodes, we can, we can figure out what exploded. And let's scroll that up a little bit here. Now we want to take the intent that we got originally and we want to modify it and send it right back. So we would do this by saying intent dot put extra. And we got to go back into our main to see what we're actually searching for. And we said type. And then we need to give it the value. Now we need to set the result. That way, Android knows that the user actually clicked the button instead of just hitting back. And we need to, sorry about that. We need to actually give it the intent back. And then just tell Android that we're finished with this activity and it's safe to close it. All right. So let's give this thing a good run here and see if our little application is working. All right. So I'm going to enter my age. And this will be a little embarrassing. I am currently 42 years old and we're going to calculate I am 252 cat years. Now we want to go to fish. I'm only 126 fish years. But if we do dog, which dogs already selected there, I am 378 dog years. That's embarrassing. That makes me feel really, really old. I'd be an old dog. So that in short is our beautiful little program for the source code for this and all of the tutorials visit my website void rums.com. They will be under tutorials. And then under, I really got alphabetized that under Android and it'll be Android five. And if you have any questions, concerns, you get stuck. Go out to the void rums Facebook group. There is 1000 plus programmers out there with all different languages. And they're actually very helpful and very nice. I've asked some incredibly stupid questions and people kind of make fun of me, but it's all in good nature. Some caveats to this program. One thing you really should do if you're going to make this a professional and throw it up in the app store is actually make, you know, the intent to give the ID and then make it auto selected and all that. But we're not going to do that because I'm running out of time and I actually have a previous engagement I have to get to. So I hope you found this educational and entertaining and thank you for watching.