Question

I have a Scroll view in my xml file there is number of layouts. there is also a Table layout in that table layout i want to add some check box dynamically. i have no idea about this how to add the dynamical check in predefine layout in xml file Please help me. thanks In advance

Was it helpful?

Solution

try this code

TableLayout Table = (TableLayout) findViewById(R.id.features);
CheckBox Box= new CheckBox(this);

        Table .addView(Box);

OTHER TIPS

To get a dynamically created checkBox into your layout you can do something like this

    TableLayout l = (TableLayout) findViewById(ID_OF_YOUR_LAYOUT);
    CheckBox box = new CheckBox(this);
    box.setChecked(true);
    box.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            //what happens when the it's unchecked or checked       
        }
    });

    l.addView(box);

This is will put the checkbox somewhere onto your layout (it doesn't matter which tpye of layout you have since they all inherit from the view class which has the method addView). If you want to specify where and how the checkbox will look you could use

    RelativeLayout.LayoutParams layout = 
            new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 200);
    l.addView(box,layout);

If you were using a LinearLayout you would use LinearLayout.LayoutParams instead of RelativeLayout.LayoutParams.

If you mess around with the different layout attributes you can simulate anything you do in xml using the LayoutParams.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top