Code example for TableLayout

Methods: addViewsetColumnShrinkablesetColumnStretchable, setTag

0
        ViewWrapper wrap=null;
        View row=convertView;
 
        if (convertView==null) {
                         
            TableLayout layout = new TableLayout(ctxt);
            layout.setColumnStretchable(0, true);
            layout.setColumnShrinkable(0, true);
             
            TableRow tableRow = new TableRow(ctxt);           
            View guts=delegate.getView(position, null, parent);        
            CheckBox cb=new CheckBox(ctxt);
             
            cb.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            guts.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.FILL_PARENT));
 
            cb.setOnClickListener(new OnClickListener() {
                @Override 
                public void onClick(View buttonView) {
                    CheckBox checkBox = (CheckBox) buttonView;
                    int listIndex = ((Integer)checkBox.getTag()).intValue();
                    boolean isChecked = checkBox.isChecked();
                    states[listIndex] = isChecked;
                    handleCheckBoxOnClick(checkBox, listIndex, isChecked);
                } 
            }); 
             
            // ! order determines indexes to get in ViewWrapper getCheckBox(), getGuts() ! 
            tableRow.addView(guts);
            tableRow.addView(cb);
            layout.addView(tableRow);
             
            wrap=new ViewWrapper(layout);
            wrap.setGuts(guts);
            layout.setTag(wrap);
 
            cb.setTag(new Integer(position));
            cb.setChecked(states[position]);    // set marked if initialized that way
 
            row=layout;