Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Dynamically add custom checkboxes to a tableLayout

What should I do to use custom checkboxes if I add my checkboxes dynamically in my code? (On the java code not on the XML files.) I'm following this tutorial, but using it I can't achieve my goal.

For example, I have a tableLayout and I want to add a checkbox for each new row I have.

Thank you all.

like image 303
fxi Avatar asked Aug 24 '26 11:08

fxi


1 Answers

If you want to add custom checkboxes try this:

StateListDrawable stateList = new StateListDrawable();
int statePressed = android.R.attr.state_pressed;
int stateChecked = android.R.attr.state_checked;
stateList.addState(new int[] {-stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_3)));
stateList.addState(new int[] {stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_1)));
stateList.addState(new int[] {statePressed}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_2)));
final CheckBox box = new CheckBox(this);
box.setButtonDrawable(stateList);
like image 154
Perun Avatar answered Aug 27 '26 03:08

Perun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!