Tuesday, June 19, 2012

Android Simple CheckBox List

Android Simple CheckBox List

I was running into some difficulty building a simple list of CheckBoxes that can be enumerated to find all the ones that are checked. There are examples that use custom adapters and so fourth, but really come on Android, get your ListView implementation easier to use. I come from a .NET background and it's a cake walk to get checked items in  a list!

Anyway, this method is simple and can work with a dynamic set of CheckBoxes.

1.) I first set up a simple String Array which contains the CheckBox Text:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Dynamic Checkbox Array</string>

    <string-array name="data_array">
        <item>Apple</item>
        <item>Orange</item>
        <item>Lemon</item>
        <item>Lime</item>
        <item>Cherry</item>
        <item>Grape</item>
        <item >Banana</item>
        <item >Strawberry</item>
        <item >Pear</item>
        <item >Plum</item>
        <item >Apricot</item>
        <item >Pineapple</item>
        <item >Mango</item>
        <item >Coconut</item>
    </string-array>

</resources>


2.) Next the layout (main.xml). It consists of a ScrollView with a LinearLayout in the Vertical orientation. I specifically give this layout a unique ID.

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

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button_click"
        android:text="Get Checked Items" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/Checkbox_Layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>


3.)  Next is the main activity onCreate()

private LinearLayout checkboxLayout;
    private String[] data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        checkboxLayout = (LinearLayout) findViewById(R.id.Checkbox_Layout);
        data = getResources().getStringArray(R.array.data_array);

        for (int i = 0; i < data.length; i++) {
            CheckBox cb = new CheckBox(getApplicationContext());
            cb.setText(data[i]);
            checkboxLayout.addView(cb);
        }

    }


4.)  Next is the button_click method which enumerates all the CheckBoxes to determine which are checked and then display their respective text in a Toast.

 
public void button_click(View view) {

        for (int i = 0; i < checkboxLayout.getChildCount(); i++) {
            if (checkboxLayout.getChildAt(i) instanceof CheckBox) {
                CheckBox cb = (CheckBox) checkboxLayout.getChildAt(i);
                if (cb.isChecked()) {
                    Toast.makeText(getApplicationContext(),
                            cb.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            }
        }

1 comment:

  1. Hi tony, i'm sankaran. I want to list contacts with checkbox and after that want to handle selected items .. can you help me thank you..

    ReplyDelete