Android Checkboxes

We'll now add some check boxes to our app from the previous lesson. We'll add three of them. These will be things you can buy. You can buy none, one, two, or all three items. The price for the items will then appear in the display area at the top. Let's make a start.

Locate the CheckBox control in the Widgets section of the Palette. It looks like this in earlier versions of Android Studio:

The CheckBox in the Android Studio palette

And later versions, the Checkbox can be found under the Buttons category:

The CheckBox control in Android Studio 3

Drag one onto your layout. By default, it will end up in the top left of your layout. Drag it below the RadioGroup and the radio buttons. Now add constraints like you did before. Add a constraint from the top of the checkbox to the bottom of the RadioGroup. You can set a top margin, as well, just like you did before. Set it to a value of 24 from the dropdown box. Add constraints to the left and right edges of the screen. Now add two more checkboxes and set their constraints.

When you add all three, your layout will look something like this:

Three checkboxes added to a layout

Adding checkboxes can be a bit fiddly, so you might need a few tries before you get it right. (It's often better to add them to a vertical LinearLayout, but we're not too concerned about having a great design for this section.)

Now select each checkbox in turn and set the Text and ID properties as follows (you can select a checkbox in the Component Tree):

Checkbox One

Text: Java Course - £30
ID: checkboxJava

Checkbox Two

Text: Android Course - £20
ID: checkboxAndroid

Checkbox Three

Text: C# NET Course - £10
ID: checkboxCSharp

Your layout will them look like this (we won't worry about the alignment being out):

Checkboxes on an Android layout

Now let's do some coding for checkboxes. We'll do that in the next lesson below.

Back to the Android Contents Page