 Hi, this is Christian. Welcome. In this video, we're going to look at radio buttons in Android Studio. So here is the layout I'm going to use for this illustration. We're going to just focus on the radio buttons, how we select the radio buttons, how do we know which one is checked, and if so, we're going to perform some operation. So I have three fields up here just to enter three numbers. And we're going to do an operation based on which one of these radio buttons is selected. If it's the smallest one is checked, then we're going to get the smallest number from the list here, the average, and the largest as well. I'll put the result down here. Now, if any of these fields is left empty, we're going to just generate an error message to the result and also we'll maybe throw a toast message in the bottom of the screen here as well. Same thing with the buttons here. If none of the buttons is checked, then we also want to throw a message. So each of these buttons has an ID already. As you can see down here on the left, all these are IDs. Red, smallest, average, red, largest, and they are part of a radio group called RDG Group 1. I put that in here. So make sure that they are in a radio group and they must be nested inside a radio group in order for this to work. So let's see if I run this first and see what it looks like. Right now there's nothing attached to it. So you can see that when I enter numbers in here and none of these is selected yet. We're going to do selection and so on. So that is our layout. Let's go back. So let's go to the program here. Now I already have the functions created for us. So I'm not going to go through all of that, but I'll explain what they are and so on. So here in the class, I created a radio group object. I haven't initialized it yet. Do it down here. And then basically in the uncreated function, I established a reference to the radio group by going to its ID and you assign that here. I have a text result for the text view to output the data. Again, we reference that down here in the uncreated function. And then the text inputs. These are three input fields. At a text, I create an array of the inputs and then I instantiate it down here using the new edit function down here. Create three references to the input 1, 2, and 3 and assign them to the elements of the array called text inputs. And then these are just the messages I will output to the text field if we left the fields empty or if you don't select a radio button. Okay, so let's go down here a little bit. This is the clear button. I went ahead and created the event listener for the clear button. All it does is basically it calls a clear function called clear all and a clear all function is way down here at the bottom. And let me go down here to the bottom. All it does is basically clear the check marks for the radio button. Now you go to the radio group here has a function called clear check. This function will clear all the radio buttons in that group. Okay, that means it will uncheck everything. Usually there's only one anyway, but it's easier this way because you have to target which one. You don't really care which one. You know that it will uncheck all of them. And then also, you know, set the text for the result to be blank. And we also go through the loop of the array of these inputs. Those three input boxes and also set them to empty. Okay, so that's what it's clear function does. Now back up here again. So we're going to do is this to do here. Okay, this is the one that we're going to submit and do the calculation. So here's a function to get the input data for from those three inputs. I'm going to create a array of data, just throw three, three data, the size of the text, the length will give you the size of that. And I'm going to loop through the text inputs array, which we created up here. Right, remember here, loop through these three elements here and get the data and they convert them to double. And so we have a data of double numbers and then we return that to wherever it's been used. It's empty. Again, it checks all these three fields. Just make sure that none of them are empty. If anyone is empty, they're going to return true and this function is now true. So therefore, we, you know, I'll put this message up here, down here. And this is the error message for the reader button. If it's not checked, the error message, if the, if any of the fields is left empty, this is the function to return the smallest of the data. So again, we call it getInputData up here, this is the one here. Right, we turn the data of three numbers and then we're going to just use the array, sort the array and we get the smallest one, which is the zero index of the data we set to the text result. The largest is kind of the same thing here except we get the last one on the array list. So it's the data length minus one gives the last index. Okay, the average, we just basically loop through the data and we add the sum of those three elements and then we divide that by the length of the array to give you the average of that data set to the result. Okay, so that is pretty much all set up. Now let's go and implement this function up here or this to do here. Now this to do here if you're not familiar, it's not a comment. It is a comment, but it's a little bit more than that. The word to do here is all uppercase or lowercase doesn't matter. No space in between. If you go down to the bottom, down here you see the to do tab. If you click on that, it will tell you that you have some tasks to do. So it's really, really useful for, you know, pseudocoding. You can write your pseudocoding and say, okay, I need to do here. I need to do something here. You put to do here. You put to do everywhere and it will track that for you. So just make sure you don't forget, right? So really, really useful. So now I'm going to do this. I'm going to remove this now. We're going to create a submit button. And then in here, I'm not going to use this button again. So I'll do exactly the same way. I just combine or join the function. So we're going to find the button called submit. Okay. We're going to just join the set on a click listener. This is the one, click tab. And then the new view, click listener, click tab. And here we go. So I'll do this in two ways. Okay. We'll do this way first. So the first thing is you need to get references to each of those three buttons. So you can do something like this. Radio button. We'll just call it the same thing, red smallest. Same name. Radio button and find our ID dot red smallest. I'm going to duplicate this two times. Control D would do that for you. And we'll get this the largest. And this is the average. And I'm just going to copy over here. Okay. So I got my three references. Okay. Now, so I need to know which one is checked. Okay. If they're checked at all. If they're not, not always checked. Then we want to throw the message. Radio button message down here. Okay. So I would just use the if block. If the red smallest, we'll do that first, is checked. It's a function called is checked. If that is checked, then go ahead and call the on smallest function. Okay. Else if the red largest is checked. I mean, the order doesn't matter here. I just chose that because it just makes sense to me. On largest. And then if the red average, oops, is checked. Then we're going to call the on average. Else that means none is checked. Therefore we're going to throw the message radio message button. Okay. I think that's all we had to do here. So let's go and run this and see what happens. Here we go. Okay. So first thing is I want to do a submit. Okay. You see that it says radio button. You need to select the radio button. I think we forgot to check these up here. So if I put one here, here, right. It says I have to check the radio button. So if I check one. Okay. So you know that it failed because I did not check the empty there first. So let's do that. I forgot to do that here. So before I even do this, I guess it doesn't matter what you do first. Before I do this at all, let's go up here and check the is empty. Okay. It's empty field. We call the function first. It's function here down here. Check to see if the text fields are empty or not. Right. We turn true or false. If it's empty, then we're going to call the empty input message. Okay. Else, go ahead and then proceed with the rest, which is the radio button. Okay. So that piece was missing. Okay. So let's try again. Here we go. If I click submit. Okay. Great. It says input fields cannot be empty. And also we can see the toast message down here as well. So I can select this if I want to, but let's do one at a time. So one, if I click submit, again, the message is still showing up. So that's good. If I go over here and put four. Okay. So it does work. Okay. Right. So now those are filled, but then now it says the radio button needs to be selected. So we'll pick the smallest one and so we go. We get the smallest. Now I put here a double. So we had a decimal value because I'm allowed to put decimal numbers like this. So if I do again, smallest 1.5, the largest would be four. And the average is something between. Okay. So it works beautifully. Right. Now if I clear this, everything's clear. Right. And that's what I want. So again, it works exactly what I wanted. Good. So next part is this right here. I did this way because, you know, you check each one of them this way. What if you have, you know, this is not so bad. I have like five of them or 10 options. Do you go through every one of them or not? Right. That's a lot of work here. So instead of going this way, you can do a different way by going through the radio group. Okay. The radio group here controls all the buttons in there. And it will, it has a special function, special function you can use that will return only the one that has been checked. Okay. So let me go ahead and comment this out for now. We'll do it a different way. Okay. I'm going to comment this out. Hide this and go, actually, I don't need this either. Okay. I don't need that either. So just do right above here. So first, we're going to just grab the ID from the radio group. Okay. So I can say int radio ID. We just need one ID from the radio group. And it's called radio group dot. There's a function called get check radio input here. This is the function we need. Okay. This function here, as you can see, mouse over this, it says it returns an integer, returns the identifier of the selected radio button. Okay. If it's not empty, that means if it's not checked, if none of them is checked, it returns a negative one. Okay. That's why we assign that to the ID of red. And they're going to check this red ID against the ID of each of those buttons. And these buttons are, the IDs are here. Red, smallest, red average. And the IDs are actually numeric. It's a 10 digit character. I mean a number ID. But it doesn't matter, right? So then we can use a switch in this case. Switch this red ID. Okay. And if it, in case, if it matches the R dot ID of red smallest, then on smallest, we break. Okay. Case it matches the R ID of red large. And then we call on largest. And then break. Case it matches the R ID of average. And then we call the on average break. Case if it doesn't match anything at all, then it's the default. Okay. We call it default. And we call the error message, which is again, radio button message. Okay. Again, before we do this though, we want to check if it's empty, right? If, if it's not empty field, then, well, let's go empty versus fine. If it's not empty, it doesn't matter. If it's empty, then go ahead and the empty message else. And then we do that. Okay. So kind of same set us before, except we're using the radio group here and instead. So it should still work if I don't have any syntax errors here. Let's, let's go and run this again. Here we go. And you can see it worked just fine input fields cannot be empty. Put here. It's less good. Put here. And then select the reader button, average. And then here we go. So that's it, guys. That's how you do it. I gave you, you know, show you had two options. Of course, the other ways as well, lots of ways you can even do so that when you click a selection, it would perform that operation right away. But in this case, I'm using a submit button to do that. So again, if any questions, let me know. Just remember that if you do this way, it's actually easier this way than the other way down here where you have to, you know, create this many buttons for that. Okay. Well, thanks for watching. I'll see you guys in the next video.