Home and Learn: Android Course


Coding for Android Checkboxes

In the previous leson, you added checkboxes to your layout. We'll now write code to detect which of the checkboxes are selected.

Again, we'll set up a method for our checkboxes. Then we'll point each checkbox to this method. Add this method to your MainActivity.java code, just below the first one:

public void getSelectedCheckboxes(View view) {

}

We've called our method getSelectedCheckboxes, but you can call it almost anything you like. In between the round brackets, you need a View object. Ours has the variable name view.

The first thing to do is to get a reference to the checkboxes on the layout. We can use findViewById for this. Add these three lines:

CheckBox checkJava = (CheckBox) findViewById(R.id.checkboxJava);
CheckBox checkAndroid = (CheckBox) findViewById(R.id.checkboxAndroid);
CheckBox checkCSharp = (CheckBox) findViewById(R.id.checkboxCSharp);

(If you get red text for CheckBox, import the library with the shortcut ALT + Enter. If this doesn't work, add the line import android.widget.CheckBox; to the top of the code.)

Your coding window should look like this (we've contracted the onCreate and getSelectedRadioButton methods):

Java code that gets references to Android checkboxes

You can test if a checkbox is selected with the inbuilt method isChecked(). We'll do so it in a series of IF Statements. Add a String variable to hold a message:

String displayMessage = "";

Now add the first IF Statement:

if (checkJava.isChecked() ) {

displayMessage = displayMessage + checkJava.getText() + "\n";

}

If the java checkbox is checked then we build up the displayMessage string. For the moment, we'll only get the text that is on the checkbox. We also add a newline character ( "\n" ).

Now add IF Statements for the other two checkboxes:

if (checkAndroid.isChecked()) {

displayMessage = displayMessage + checkAndroid.getText() + "\n";

}

if (checkCSharp.isChecked()) {

displayMessage = displayMessage + checkCSharp.getText() + "\n";

}

We also need to display a message in the TextView. We can do that as we did before:

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

myDisplay.setText(displayMessage);

This time, the myDisplay TextView has the displayMessage variable between the round brackets of setText.

Here's what your getSelectedCheckboxes method should look like:

Java method for Android checkboxes

Before you can try it out, you need set the onClick property of each checkbox to point to your new method. You did this for the RadioButtons, and the process is the same.

When you've pointed each checkbox to your method, run your app and try it out. Select checkboxes and watch what happens to the display.

An Android app showing selected checkboxes

 

In the next lesson below, you learn how to add and code for a Toggle Button. We'll make this a button for accepting terms and conditions.

Back to the Android Contents Page

 


Email us: enquiry at homeandlearn.co.uk