Reputation: 1463
I have an Android astronomy app where I need to tint the UI reddish for use at night. Although I have a scheme that works well for many (most??) UI elements, I'm having trouble with the CompoundButtons: CheckBox and RadioButton.
The basic idea is to retrieve the Drawable for the UI element and if it has one to set a color filter on it. My problem is finding the appropriate Drawable for the compound buttons. I would think that getCompoundDrawables() would be what I'd need, but the returned array for the 4 drawables always contains nulls for the 4 elements.
Here is the recursive code I call on the top level view to try to colorize the UI elements.
public static void setNightVisionBkg( View view )
{
if ( view instanceof ViewGroup )
{
Drawable drawable = view.getBackground();
if ( drawable != null )
drawable.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
ViewGroup group = (ViewGroup) view;
int numChildren = group.getChildCount();
for ( int i = 0; i < numChildren; i++ )
{
View v = group.getChildAt( i );
Drawable d = v.getBackground();
if ( d != null )
d.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
if ( v instanceof ViewGroup )
{
setNightVisionBkg( (ViewGroup) v );
}
else if (v instanceof CompoundButton)
{
CompoundButton compBtn = (CompoundButton)v;
Drawable drawables[] = compBtn.getCompoundDrawables();
for (int j = 0; j < drawables.length; j++)
if (drawables[j] != null)
{
drawables[j].setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
}
}
}
}
}
Note that it is the latter part where it is getting the Drawables for the CompoundButton that fails to work (all drawables are null).
Any thoughts on how to do this? I know I could set my own custom drawables, but I'd prefer to use the standard ones and just set a colorFilter if possible.
Upvotes: 2
Views: 2413
Reputation: 1463
I solved my problem in a slightly different way. Ended up subclassing CheckBox (and RadioButton). In the subclass I override:
protected boolean verifyDrawable( Drawable drawable )
and in this method I set the colorFilter on the drawable. Works great.
Upvotes: 2
Reputation: 21377
You don't progress "normal" non-ViewGroup Views. Furthermore your testing for ViewGroup in a for loop which can ONLY be ran if the view is a ViewGroup.
Upvotes: 0