1

Is there any way to set a drawable xml asset in a chip programmatically?

Just playing around with chips, not forced to use them actually. I can easily swap, but i was wondering if that's possible or not.
In my project i have a drawable folder which looks like so

Screenshot of drawable folder

This folder (maybe is standard, maybe is not, dunno: I'm pretty new to Android Studio and that folder got auto generated) has a bunch of xmls in it, which I managed to use in views.

Now, I was playing around with chips in a fragment, and I am failing on finding some way to set the chip checked icon from code (which is a tick by default). I was trying to find some way to go like Chip.setIcon(R.drawables.my_xml_asset_name).

I know I can set that via xml, so please don't suggest that.
API version: Level 34 (Upside Down Cake)

1 Answer 1

1

The checked icon can be specified by using the setCheckedIcon (takes a Drawable)/setCheckedIconResource (takes a drawable resource ID) methods of Chip, which according to the documentation would be overlayed on top of the icon.

yourChip.setCheckedIconResource(R.drawable.ic_custom_checked_circle_24dp)

A list of other supported XML attributes and their programmatic variants are as quoted below (source):

Checked icon

If visible, the checked icon overlays the chip icon.

Element Attribute Related method(s) Default value
Icon app:checkedIcon setCheckedIcon/setCheckedIconResource/ getCheckedIcon @drawable/ic_m3_chip_checked_circle
Visibility app:checkedIconVisible setCheckedIconVisible/isCheckedIconVisible true (input, filter)
Color app:checkedIconTint setCheckedIconTint/setCheckedIconTintResource/getCheckedIconTint null
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.