Coding for Android Radio Buttons

Android Widgets: 1 2 3 4 5 6 7 8 9

We now need to write code to detect which radio button was selected. What we'll do is to write a method, and then point each button to that method. (In a later, you'll see a different way to detect button clicks when we explore listeners.)

Go to your MainActivity.java file and set up the following void method:

public void getSelectedRadioButton(View view) {

}

Your MainActivity code window should look like this:

Java method for a radio button

We've called the method getSelectedRadioButton, but you can type a different name, if you want. In between the round brackets of the method you need a View object (ours has the variable name of view). A View is one of those widgets you've been adding to a layout. When this method is called from a radio button selection, the radio button itself will end up in the view variable.

The next thing to do is to have each radio button call this method. So go back to your activity_main.xml tab. In the designer, highlight the Credit Card radio button. In the properties area on the right, scroll down to the bottom and click the link that says View all properties. Locate the onClick item and click the dropdown box. From the dropdown box, select your new method:

The onClick property

Now do the same for the other two radio buttons so that all three radio buttons are pointing to the method you added.

Go back to your MainActivity code. Enter the following line in your getSelectedRadioButton method:

final RadioGroup radGroup = (RadioGroup) findViewById(R.id.paymentMethod);

Here, we are getting a reference to the RadioGroup that we called paymentMethod. Once we get a reference to the whole group, we can then get individual radio buttons in that group. (The final keyword at the start, incidentally, means that the radGroup variable won't change - it's a constant.)

The next line of code to add is this:

int radioID = radGroup.getCheckedRadioButtonId( );

This sets up an integer variable that we've called radioID. What we need to do is to get the ID of the radio button that was selected. To do this, there is an inbuilt method called getCheckedRadioButtonId. You call it on a radio group variable that you've set up previously, radGroup for us.

The next line drills down even further to get at an individual radio button, using that radioID. So add this line:

RadioButton singleButton = (RadioButton) findViewById(radioID);

We're setting up a RadioButton object called singleButton. After the equal sign, we need to find the view by id. The ID that we need is stored in that radioID variable. Doing things this way means that we'll be able to get the text on a single radio button.

Your code so far should look like this:

Java code getting references to  layout widgets

(If you get any red text it means that an import line needs to be added. Don't forget that the shortcut to add an import line is ALT + Enter. If this doesn't work, just add import android.widget.RadioButton; to the top of your code.)

Almost there. The only thing left to do now is to display which radio button was selected. We'll display this in the TextView at top of our app.

Add the following line to get a reference to the TextView:

TextView myDisplay = (TextView) findViewById( R.id.displayArea );

Remember, we called our TextView displayArea. We're just getting the ID of this TextView and putting it into a variable called myDisplay.

The final line to add is this:

myDisplay.setText( singleButton.getText() );

As the text for setText we're using singleButton.getText(). This obviously gets the text from singleButton and transfers it to the TextView called myDisplay.

The whole of your code should look like this:

Java code for a radio button selection

You can try it out, now. Run your app in the Emulator or on a real phone. You should see this:

Android App running in the Emulator

Select a radio button and the text from that button should appear in the display area at the top.

 

In the next lesson, we'll add some checkoxes to this layout. You'll then see how to write code to get which checkboxes were selected.