 Hey everybody, this is Brian. Welcome to the fourth Android tutorial. We're just going to go ahead and start a new project here and we're just going to call this app 4. We're going to leave the default SDK and we're going to leave the empty activity and just hit finish. And Android Studio will calculate the alignment of every star in the known galaxy. I don't know what this thing does in the background sometimes. And we're going to go to layout. And what we're going to do today is we're going to kind of explore controls. And we're going to make a very simple application that has no benefit of no value. You're not going to make a million dollars off this thing. We're going to say pet order form. And we're just going to drag and drop a toggle button on here. Let's grab a checkbox or name that checkbox one. And we say alive. A little bit morbid, but some people will actually order dead pets. Like if you have a snake, you can order dead mice or something like that. Then we're going to throw a radio button down. And we're going to throw another one down. And I want to show you a couple of tricks here. We're going to intentionally make this not work. And then we're going to grab a plain text. And let's call this see. I don't know if you guys can hear my stomach growling. I'm like, now when you change the name, you notice how it says update usages as well. And you're kind of wondering what usages, what are you talking about here? Everything in Android is based off the ID. So you notice how there's these arrows in the background here. That's called the alignment of the control. We're aligning that control to that control to that control. Now, because we're changing the ID of dog, radio button underneath it is also going to need to reference that. And I'll show you what I mean. We're just going to hit yes. Now notice how it says, RDO dog. We're going to flip over here. Now in this guy right here, notice how it says right alignment, align in RDO dog. And we not change that suddenly that radio button would have no idea what to align to. You can tell it's doing it on an ID because it says at plus ID. This may be voodoo magic to you at this point. I know sometimes I look at this and scratch my head and wonder what it's trying to do. Just know that it's aligning to something that has an ID. That's why it's really important that every control has an ID. And we're going to change this one to cat. We're going to say RDO cat. And yes, we want to change the references. Drag that down a little bit. We're going to grab one more radio button and call this fish. We've got this little guy. And we're going to call this TXT name. And we're going to grab a button and just drag and drop it right at the very bottom here. And we're going to call this order. It's funny. I wasn't hungry until I started doing this video. And then suddenly I'm like starving. Line that a little better. There we go. So I've intentionally made this application not work the way it's supposed to because I want to show you a few things. But we're going to run this and I'll show you exactly what I'm talking about. We're going to start our emulator here. Emulator takes forever and ever and ever. Come on, you can do it emulator. There we go. So we have our little toggle button. And this is a mixture of a button and a checkbox. A checkbox. You can just put a little check on it. So it's on or off. On, off, on, off. So a toggle button is kind of a mixture between a checkbox and a normal button. Radio button allows you to only choose one option. Now watch this. Notice how I said you can only choose one option, but I just chose all three. So if you want to use a radio button, you have to put it in what's called a radio button group. And then the text box, you know, is just exactly what you think it would be. Let's get rid of that virtual keyboard. And let's, we're going to fix these radio buttons real quick here. And let's go down here. And let's actually fix our monitor first because we've got all this nonsense. We're just going to put our log tag in there. All right. So you can see over here we have our component tree. We have a palette and a component tree. And when we click on something, we have properties. So it's pretty simple. You have your, your palette, you can drag and drop. You have your component tree, which are things that have already been dragged and dropped. And notice as you select one, the selection moves around. So like we're going to select the order button and it selects the order button. It shows you the guides and everything. And it also does all the properties in here that you can graphically manipulate. So you can make that bold if you wanted to or a talent would probably be easier to see in the video, maybe not, but, uh, so what we need to do is we need to fix these radio buttons and make them work the way they're supposed to. So we're going to scroll down and we're going to find radio group. And instead of dragging and dropping onto the activity, we're going to drag and drop down into the component tree. And we're going to put it right there. Now we need to take these radio buttons and move them into the group. We're just going to click and drag them in and then click and drag them in. Now notice how when we did that, they now are moved over here. This is something that will just infuriate you about Android programming, uh, because now everything's based off the ID. So we need to give this an ID, radios. And now instead of trying to grab this thing, because we'll end up grabbing the checkbox, we're just going to grab it here and we're going to move it. And then we're going to grab our order button. We're going to move this to the very bottom here. Whoops. Let's just try doing it here. Sometimes this doesn't want to, yeah, see, you can easily break things by doing this. So let's actually control Z, get back out of there. And we're just going to grab this and move that whole thing up one. Now button order should be a little more manageable. We should be able to go like this. Maybe not. That's a little better. Not really. It's very frustrating. So as you can see, that's not working well for us. So we've got our ID here and we're just going to select this guy and go find it. And here is our order button. Now notice how it's a line left button toggle. So this thing's aligning all the way up to this guy up here. We don't want to do that. Notice how it moved it a little bit. Now the problem is everything is now aligning incorrectly because we've got this garbage in here. So let's see what's going on. So we've got our radios in here. Let's try moving this around. That's better. And then we can move this the way we wanted it to. So just know that, oops, yeah, we didn't do it the way we wanted it to, because now we've got to get this down here. We've got to move this here. That's better. That's a lot of work with very little rewards. So just be in mind, that's something that'll really frustrate you about Android programming. So when you start moving things around, because we haven't really talked about how to modify the XML, and when you look at this, it just looks like a bunch of gobbly-gook. When you see the red underline, that generally means there's a problem and you need to go fix that. Now I'm not really worried about it too much, because my layout is the way I want it to. But if it looks good here, and there's an issue here, may not work correctly on the end device. Sometimes I've seen that. So bear that in mind. And I just understand that when you're dragging and dropping, you get really frustrated. Try dragging and dropping out of the component tree. So when you do this, you can just move it around. We will go over layouts in a future tutorial, which where are they? Yeah, layouts. Notice how you're already in a relative layout. And you can see that in the actual XML code, relative layout. So you can set different layouts. All right, now that was a lot of work. Let's kill the running app. And let's run this again and see if we can get those radio buttons to work the way they're supposed to work. All right, so our toggle button works, our checkbox works. Now we can only select one radio button. That's the way radio buttons are meant to be used. And you can have several radio button groups. So you can say, you know, what type of animal, what breed of animal, things of that nature. But we're going to keep this very simple. And we are just going to change that text. And then we're going to wire up these events. And we'll make just a very quick and simple application. All right, so let's change this to pet order form, if I could spell. I've been downloading various applications. For example, Zillow, I downloaded, what was it, match.com, because it's almost Valentine's Day. I figured I should probably go find a girlfriend sometime soon, joking. But you can see how they're built. And once you start learning how Android programming, you can see the basics of, kind of got lost there, the basics of how things are kind of glued together. And you start having those aha moments. Like I was looking at what was it, I was looking at Pandora the other day. Oh, now I know how they do that. All right, so we're going to have our tag for our log monitoring. And I had someone, and I'm sorry, off the top of my head, I don't remember who it was, but somebody said, you were declaring your controls as private static and don't do that. So I was saying, like, private static button, and we'll call it BTN order, because that was our order button. He was saying, do not do that. And I asked him why. And it was in the comments. And he said, because if you have multiple instances of this activity running, and you only have a static, meaning there's one per activity, so let's say one of them says, yes. And the other says no. Well, whichever one's up is the one it's going to take. Probably a bad example. Checkboxes would have been better if one's checked and one's not. It's going to take the original instance of it. So we're going to get rid of that. And we're just going to, through the magic of copy and paste here, toggle button. And what did we call that? BTN toggle. And then what was it? Checkbox, not protected private. You can make this pretty quick. What do we have? We had a dog, cat, and fish. And we also had, I believe it was a name, because we're going to give this thing a name. TXT name. Let's go back here. And we'll say private. Yeah, it was interesting. I never knew that about the private static with multiple activities. It just never occurred to me that it would do that, but it made perfect sense. So this is an edit text, TXT name. Now we want to, you guessed it, get the instances of those. So we're going to say, and this is why I tend to like having a naming convention, because then you can just do this. You can go make a bunch of those and grab your toggle button. You have our checkbox one toggle button button. I don't think that's a control type. There we go. And then we say radio, radio, radio. I got to remember to start eating before I make these videos. My stomach's like really not thrilled with me right now. It's like, Hey, dude, remember lunch? I remember lunch. All right. All right. So we've got all these. Now let's wire up there are two buttons here, because remember the toggle button, while you can check it on and off like a checkbox, it's also a button. So it has events in it. So let's do our button wire up here. So we're going to say beat in order, beat in order. That made no sense button order. Geez. Set on click listener. And then we're going to do the same thing for the toggle button. And then we're just going to make our little functions here. That's embarrassing. Can't type today. Did I just do that twice? I totally did. Oh my god. All right. Private. There we go. Man, I'm having like one of those Snickers moments. You see those Snickers commercials where they get like really hungry. That's kind of what I'm going through right now. And we're just going to say order. So when the toggle button is clicked, this will be fired. When the order buttons click, this will be fired. So let's just flush this out a little bit. And we want to see if the toggle is on or off. So we're going to say get text. And let's actually modify this a little bit. I want to kind of show you something about the toggle button. You can actually manipulate that text. So you can say set on text. Sorry. Set text on. Open. And you can say store is closed. So you're setting whether, you know, you want that text or that text depending on the state of the button. And then down in the do toggle function, which is when the click handler is accepted. Did I say accepted? I did. When the click handler is used, it will just say, you know, whether the store is open or closed. So let's test that out first before we go anywhere else. Run this bad boy. Probably help if we had our Android monitor up and running. So when we click order, nothing happened. Why did nothing happen? Let's see here void realms. Check our filter. That's usually the first thing that we mess up is our filter. Why is that not working all of a sudden? Hmm. Interesting. What have I screwed up? Let me give this thing a good old rebuild. When in doubt, turn it off and turn it on again. Oh, that would probably be why we never actually use our functions here. Geez. Ladies and gentlemen, this is what happens when your blood sugar gets low. Okay, let's actually do this this time. Derp. All right. So now we have on click listeners being called, which are calling our actual functions this time. That was really bad. Good thing I don't charge for these. All right. So when we click, there's our beginning order and the pet store is either open or it's closed, depending on the state of our button here. Now, you notice how when I click, our little order button moves at the bottom too. And that's due to the alignment. This is something you need to be very mindful of when you do these things. Because when you change the length of the control, the other will move with the alignment. All right. So we're going to actually be in the order and we will. All right. So if this one is checked, let's say not. So if checkbox one is not checked, we're just going to say, sorry, we don't sell dead pets. Then we want to get the radio button. So we're just going to say, if RDO cat is checked, then we're going to say, and we're just going to dog and RDO fish. And there's an easier way of doing that. But just for simplicity sake, this is a very entry level tutorial. So we're just saying, you know, we're going to begin the order. If the checkbox is not checked, we do not sell, whoops, we do not sell dead pets because remember, in here, do you want it alive? Yes or no? Because some pet stores will sell you dead mice for your lizards. And then if the cat is checked, we're going to order a cat. If the dog is checked, we're going to order a dog. If the fish is checked, we're going to order a fish. Now that's a little misleading. It's a radio button, but is checked. So when we go in here, we look at the properties of this, you notice how you get the checked right here. And so we're going to say default is cat. Now if we go to fish and we check that, you notice how it moves. Even though cat is still checked. So if we uncheck fish, cat goes back. What a radio group does is it says the last one that has been checked. So always remember that. And we want to get the name of it. Let's grab this guy. And we want to get the name. So I'm named. We're going to do get text to string. And when we run this, let's show our monitor, show our simple function here. So let's see if we can do this. We're going to order. And it says, sorry, we don't sell dead pets. We actually didn't check to see that. That's embarrassing. So let's do this. Totally forgot about the toggle button. Geez. That should be a little better. Now let's actually clear this and run. Note to self, don't do these when you're hungry. Okay. So when we click order, you know, so I'm sorry, we closed, we turn this on, then it'll start, you know, the store is open and we'll be able to process this. Now it's going to say, sorry, we don't sell dead pets because we did not check this box. Let's give this name and let's call this Fido. And we can actually order a dog. And it'll say beginning order, ordering a dog named Fido, order complete. Now if we flip it over to cat, see how it says, ordering a cat or ordering a fish, things of that nature. We can close the store and then once again, sorry, we're closed. So that in a nutshell is some very rudimentary Android controls. When you go into, let's actually close this monitor here, you can see there is a lot of controls. Doing a tutorial over every single one of these would be very foolhardy, but I'm going to try and do a good chunk of them. Some of them are pretty self-explanatory. For example, you have the text fields and we just did plain text, but there's also a password, a password, numeric, email, phone number. They use regular expressions in the background, which if you don't know what regular expressions are, Reg X as it's called, or regular expressions, it's a language in itself that allows you to very quickly parse strings. So they use a lot of voodoo magic in the background to determine if it's a email or a phone number or postal address, things of that nature. There's also layouts, which a layout really isn't a control or a view per se. It is a view, but it's not really a control. It's not something the user really interacts with. We'll go over those and then you have containers like the radio button group that hold other views. Pretty neat, pretty interesting. There's a lot to it. When in doubt, usually you can just go out and you can say, okay, what is this web view, for example, and you can just Google, you know, Android web view and there's usually a million tutorials on how to do it, but we're going to cover quite a few of these. I hope you found this educational and entertaining and if you go to my website, the source code for this will be out under Android and I'll have a little notch here that says Android 4. Also, if you need additional help, you can go out to the Void Realms Facebook group. There's over a thousand of us out there in multiple languages that are willing to help out. That's it. Talk to you guys later.