how to create and validate checkbox programmatically using java in android
<p>I need to create number of checkboxes programmatically using java code and then check if one of them is checked and when I click Next Button the control will move to next page . otherwise is noone is checked then a toast msg will display. Please suggest as i am new to android</p>

<p>Here is my code:</p>

<p>public class TestValidCheckboxActivity extends Activity implements OnCheckedChangeListener{</p>

<pre><code>private RelativeLayout layoutMiddle = null;
private TableLayout layout1;
private CheckBox chk;
private  String[] resarr = {"silu", "pinky", "meera"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    layoutMiddle = (RelativeLayout)findViewById(R.id.layoutMiddleUp);
    layout1 = (TableLayout)findViewById(R.id.tableId1);
    final Button btnNext = (Button)findViewById(R.id.btnNext);
    int i = 0;
        for(String res: resarr){
            TableRow tr=new TableRow(this);
                chk=new CheckBox(this);
                chk.setId(i);
                chk.setText(res);
                chk.setTextColor(Color.BLACK);
                tr.addView(chk);
                i++;
            RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            lay.addRule(RelativeLayout.BELOW, layoutMiddle.getId());
            layout1.addView(tr, lay);
        }
        chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (arg1)
                {
                    btnNext.setOnClickListener(new View.OnClickListener() {
                        public void onClick(final View view) {
                            Log.e("1111111111","111111111");
                                if(chk.isChecked()){
                                    Intent intent = new Intent(view.getContext(),NextPage.class);
                                    startActivityForResult(intent, 0);
                                }else{
                                    Toast msg = Toast.makeText(view.getContext(), "please choose at least one option", Toast.LENGTH_LONG);
                                    msg.show();
                                }

                        }
                    });
                }else{
                    Toast msg = Toast.makeText(TestValidCheckboxActivity.this, "please choose at least one option", Toast.LENGTH_LONG);
                }
            }

        });




}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub

}
</code></pre>

<p>}</p>
