Recently, I had a requirement to change the ImageView picture to be changed based on the theme. I was using an ExpandableListView and using the ImageView as
a group indicator rather than out of the box group indicator. Since my application supported both light and dark theme, I wanted to switch the group indicators, action bar icons to light and dark versions as well.
I looked at some of the questions in StackOverflow , but none answered satisfactorily.
In the end, I found out it was fairly easy, without using any code. Here it is how:
Declare custom attributes in res/values/attrs.xml file for each of the ImageView or ActionBar icon to be changed based on theme:
In the theme file which would be res/values/styles.xml or the suitable one depending upon the version you are targetting, define the same attributes pointing to the
appropriate drawables. For example below, the Black theme is pointing to drawables related to dark theme and Light theme is pointing to drawables related to light theme.
Finally, coming to the actual layouts, point the relavant attributes to the attributes you have defined rather than directly to the drawables. For example
in case of ActionBar icons:
Or in case of ImageView:
The exciting part is that this concept can be applied to any control or any attribute :)