Tuesday, November 1, 2011

Using Android Checkboxes

In almost any settings window/screen you will probably need to use a check box, to "tick" something on or off. We are going to do some easy check boxes to help make a selection of what kind of fruit we would like to eat. Once the box is selected we will press a "save settings" button to show our selection.

I will assume you already know how to setup a project already. I will be (and have always been) using Eclipse as my IDE. We will have two screens, the first screen will have a button to allow us to move to our settings screen. The next screen is our settings screen, which will let us choose out fruit as mentioned earlier. Below is our first screen in graphical mode in our IDE.

After placing our button on the screen we now make a new activity for our settings screen and a new XML file for our settings screen. If you do not know how to make  a new screen I advise you refer to the other tutorials on this website.

 The second activity, I named it Settings.java and the XML file is named settings.xml. Here is the code for both files, just in case you need a refresher.

package com.lerie.chkboxdemo;
import android.app.Activity;
import android.os.Bundle;

public class Settings extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

and don't forget to add the new screen to your AndroidManifest.xml file:

      <activity class=".Settings"

As of now, the settings button won't do anything until we set the listener event up. It is quite simple, you can find a tutorial on this website for that as well, it looks a bit like this..

        Button next = (Button) findViewById(R.id.button1);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Settings.class);
                startActivityForResult(myIntent, 0);

When the button is clicked it will show the settings screen. Let's design our settings screen now. We will add three checkboxes and a button. If we wanted to group the checkboxes we would simply use RadioButtons instead of checkboxes to make it much easier. With checkboxes we can make multiple selections.

When we make a selection then press the "save settings" button it will pop up a message (with Toast) and show us what we selected then move back to the Main screen we started on. So we need to get the values or at least the state of the checkboxes. It's pretty blatant and straight forward. We will check the "ischecked" method, which is a bool.

First, let's get the checkboxes..

            CheckBox apple = (CheckBox) findViewById(R.id.chkApple);
            CheckBox orange = (CheckBox) findViewById(R.id.chkOrange);
            CheckBox peach = (CheckBox) findViewById(R.id.chkPeach);

After that we can check the methods for the checkboxes..

                    Toast.makeText(Settings.this, "You chose an apple.", Toast.LENGTH_SHORT).show();
                    Toast.makeText(Settings.this, "You chose an orange.", Toast.LENGTH_SHORT).show();
                    Toast.makeText(Settings.this, "You chose a peach.", Toast.LENGTH_SHORT).show();

and then start our other activity..

                Intent myIntent = new Intent(view.getContext(), ChkBoxDemoActivity.class);
                startActivityForResult(myIntent, 0);

No comments:

Post a Comment