I’ve added a widget layout to my preference screens showing an arrow icon, to indicate to users that there are more preferences behind a given PreferenceScreen.
However, I notice when a PreferenceScreen is not enabled it is darkened. Is there a way I could also change the widget layout being used, or make it stateful, so that I could use a faded icon when my PreferenceScreen is disabled? Similar to how a state list drawable can be applied as a button’s background?
tl;dr: How can I change the icon displayed when an instance of PreferenceLayout is disabled?
This can be done solely with XML in 3 steps.
First, create a stateful drawable that can change according to whether the widget using it is enabled, disabled, etc. So statelist_example.xml kept in the drawable folder:
Then define your own layout to use this drawable in the layouts folder. This will become the “widgetLayout” for the Preference. So layout_example.xml:
Third, specify the widgetLayout in every preference to use this layout:
So basically you have your Preference reference a Layout which references a stateful drawable.
Android Docs on Statelist Drawable.
Android Docs on Preference widgetLayout.