Checkbox Text List :: Extension of Iconified Text tutorial
What you will learn: How to create a list of items with Checkboxes
Difficulty: 2.0 / 5
What it will look like:

Description:
This tutorial grew from another tutorial here which dealt with making lists with icons attached to each list item. (IconifiedTextView tutorial).
1. In order to make a List which includes checkboxes, we need to modify a few things. In CheckBoxifiedText.java, we made a String to hold the text of our list item. We also need a boolean value to keep track of the status of the checkbox (checked = true, unchecked = false). The constructor will initialize the checkbox to be checked or unchecked.
Using java Syntax Highlighting
- public class CheckBoxifiedText implements Comparable<CheckBoxifiedText>{
- private String mText = "";
- private boolean mChecked;
- public CheckBoxifiedText(String text, boolean checked) {
- /* constructor */
- mText = text;
- mChecked = checked;
- }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
2. Second, we need to look at CheckboxifiedTextViewAdapter.java. We need to add code to support our checkbox. (src.getChecked() will tell us whether our box is checked or not.
Using java Syntax Highlighting
- public View getView(int position, View convertView, ViewGroup parent){
- CheckBoxifiedTextView btv;
- if (convertView == null) {
- btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
- } else { // Reuse/Overwrite the View passed
- // We are assuming(!) that it is castable!
- CheckBoxifiedText src = mItems.get(position);
- btv = (CheckBoxifiedTextView) convertView;
- btv.setCheckBoxState(src.getChecked());
- btv = (CheckBoxifiedTextView) convertView;
- btv.setText(mItems.get(position).getText());
- }
- return btv;
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
We also want to add some methods for doing things like getting the state of the checkbox, or selecting all the items.
Using java Syntax Highlighting
- public void selectAll(){
- for(CheckBoxifiedText cboxtxt: mItems)
- cboxtxt.setChecked(true);
- /* Things have changed, do a redraw. */
- this.notifyDataSetInvalidated();
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
3. Open up CheckBoxifiedTextView.java. We need to set up where we want the checkbox to be located, the text location, and whether the box is checked or not.
Using java Syntax Highlighting
- public CheckBoxifiedTextView(Context context, CheckBoxifiedText aCheckBoxifiedText) {
- super(context);
- /* First CheckBox and the Text to the right (horizontal),
- * not above and below (vertical) */
- this.setOrientation(HORIZONTAL);
- mCheckBoxText = aCheckBoxifiedText;
- mCheckBox = new CheckBox(context);
- mCheckBox.setPadding(0, 0, 20, 0); // 5px to the right
- /* Set the initial state of the checkbox. */
- mCheckBox.setChecked(aCheckBoxifiedText.getChecked());
- /* At first, add the CheckBox to ourself
- * (! we are extending LinearLayout) */
- addView(mCheckBox, new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- mText = new TextView(context);
- mText.setText(aCheckBoxifiedText.getText());
- //mText.setPadding(0, 0, 15, 0);
- addView(mText, new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
4. Finally, we can look at our ListActivity code which will be using the classes we just made. In the attached source code, this is the Checkbox.java file. For this example, i am using an array of strings which contain the text we want in each list item. We create a checkboxlist adapter, cbla, first.
Then, we loop through each item in the array and add it to the list adapter (using cbla.addItem()). I am setting all the checkboxes to initially be unchecked by passing the value of FALSE.
Using java Syntax Highlighting
- public class Checkbox extends ListActivity {
- /** Called when the activity is first created. */
- private CheckBoxifiedTextListAdapter cbla;
- // Create CheckBox List Adapter, cbla
- private String[] items = {"Box 1", "Box 2", "Box 3", "Box 4"};
- // Array of string we want to display in our list
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- cbla = new CheckBoxifiedTextListAdapter(this);
- for(int k=0; k<items.length; k++)
- {
- cbla.addItem(new CheckBoxifiedText(items[k], false));
- }
- // Display it
- setListAdapter(cbla);
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Thats pretty much it. Not much work! Attached are all the source files you will need for this tutorial. I have also added functions such as select all, and deselect all which will come in handy.
Enjoy,
Daniel







