Preference myPreference;
...
Drawable originalIcon = myPreference.getIcon();
myPreference.setIcon(android.R.drawable.btn_star);
...
myPreference.setIcon(originalIcon);
The above code will change the icon to a preference, and then later restore it.
If the preference does not have an icon, the text for the preference is shifted right and the icon is added (getIcon returns null). Calling setIcon with null for the Drawable does not remove the icon. How can I remove the icon and shift the preference text left to its original position.
OK, one way to do it is to define a null icon drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list />
and then use:
if (originalIcon == null) {
myPreference.setIcon(R.drawable.my_null_icon);
}
else {
myPreference.setIcon(originalIcon);
}