Coding for Android Checkboxes

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

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 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 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

However, we'd like to display a total price for the courses selected. So how can we do that?

First, set up two more variables, one String and one int. Put them just below the displayMessage variable and above the IF Statement:

String totalMessage = "Total Sales =";

int totalSales = 0;

Before the final curly bracket of the first IF Statement, add this line:

totalSales = totalSales + 30;

(The number 30 is the price 0f the Java course.)

Add this line before the final curly bracket of the second IF Statement:

totalSales = totalSales + 20;

And this line before the final curly bracket of the third IF Statement:

totalSales = totalSales + 10;

Your new variables and IF Statements now look like this:

Java logic to get total sales

The setText line at the bottom for the TextView needs to be changed. It's this, at the moment:

myDisplay.setText( displayMessage );

However, we need to do some concatenation. We need to join whatever is in diaplayMessage variable to the totalMessage and totalSales variables. We could just do this:

myDisplay.setText( displayMessage + totalMessage + totalSales);

If we did this, though, Android would complain. It would tell us not to concatenate inside of setText. Instead, we can use String.Format. Like this:

String.format(Locale.ENGLISH, "%s %s %d", displayMessage, totalMessage, totalSales)

In between the round brackets of String.Format we first have this:

Locale.ENGLISH

Apparently, not specifying a Locale is a major source of bugs. After the word Locale, type a dot. You'll then see a list of Locales to choose from. We've went with ENGLISH, but select any from the list, depending on what language you'll be targeting.

The actual string format is this:

"%s %s %d", displayMessage, totalMessage, totalSales

The two %s symbols at the start set up string values. The %d is used to set up an integer value. We then have our three variables, two String variables and an int one.

The whole of the setText line to add is this (it's rather long):

myDisplay.setText(String.format(Locale.ENGLISH, "%s %s %d", displayMessage, totalMessage, totalSales));

The whole of your code should now look like this (we've spread the setText part over two lines):

Android Java code for radio buttons and checkboxes

Try it out. Run your app again and select some checkboxes. You should see something like this in your display area:

Android app showing checkboxes selected and total sales calculated

Deselect a checkbox and the correct total should be displayed.

 

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.