Here is what I did for my project:
LinearLayout llsomething = new LinearLayout(this); Checkbox cbSomething = new CheckBox(this); TextView tvSomething = new TextView(this); tvSomething.setText(R.string.something); llsomething.addView(cbSomething); llsomething.addView(tvSomething);
EDIT:
You can also make your layout clickable to make it more "natural":
llSomething.setClickable(true); llSomething.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LinearLayout layout = (LinearLayout) v; CheckBox check = (CheckBox) layout.getChildAt(0); if(check.isChecked()) check.setChecked(false); else check.setChecked(true); } }
source share