1

In my application Im creating 10 checkboxes during runtime and I want to set the following xml attributes programmatically. I've been searching everywhere and cannot find any solution and I'm pretty new to android development. Would appreciate any kind of help!

android:button="@null"
android:drawableTop="?android:attr/listChoiceIndicatorMultiple"

I've been researching on how to access the android:attr file without any success.

HagelHarry
  • 15
  • 3

2 Answers2

2

You can use the setButtonDrawable to assign android:button="@null"

CheckBox checkBox = findViewById(R.id.checkbox);
checkBox.setButtonDrawable(null);    

You can the setCompoundDrawablesRelativeWithIntrinsicBounds() method to assign the drawable. Use this code to get the value of ?android:attr/listChoiceIndicatorMultiple:

TypedValue typeValue = new TypedValue();
 getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple,typeValue, true);
checkBox.setCompoundDrawablesRelativeWithIntrinsicBounds(0,typeValue.resourceId,0,0);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0
button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

for drawable

setButtonDrawable(Drawable d)

for button

Djaf
  • 256
  • 1
  • 6
  • Thanks! The problem is that I can't access the attrs.xml in my code and I have no idea how to. It looks like this right now: checkbox.setCompoundDrawablesWithIntrinsicBounds(0,android.R.attr.listChoiceIndicatorMultiple,0,0); Which doesn't work – HagelHarry Oct 09 '19 at 19:04
  • you need to retrieve drawable and you need context for that try to use something like this Drawable img = getContext().getResources().getDrawable( R.drawable.smiley ); checkbox.setCompoundDrawablesWithIntrinsicBounds( null, img, null, null); from https://stackoverflow.com/questions/4502605/how-to-programmatically-set-drawableleft-on-android-button – Djaf Oct 09 '19 at 19:21