mercredi 26 août 2015

Tinting a checkbox using with setButtonTintList not showing second color

I am trying to get Tinting (at runtime) to work on Lollipop but it seems to ignore the second color passed in. By default you will see when a checkbox is not checked the color is white (on a dark theme, colorControlNormal) when checked it's the accent color e.g. blue.

It's not recognising the second color that I am passing in.

 int offColor = getResources().getColor(R.color.red);
 int checkedColor = getResources().getColor(R.color.green);
 ColorStateList sl = createCheckedColorStateList(checkedColor, offColor); 
 checkbox.setButtonTintList(sl);

   private ColorStateList createCheckedColorStateList(int checkedColor, int offColor) {
         return new ColorStateList(new int[][]{
                 new int[]{android.R.attr.state_checked}
                 ,new int[]{-android.R.attr.state_checked}
                 }
                , new int[]{ offColor, checkedColor}
         );
     }

It just remains one of the color passes in checked on unchecked. So if the checkbox was initially checked after tintng it will go to the off color passed in (red in the example) when unchecked, but checking it again will remain at the off color (red).

I even tried a few more complex ColorStateList and the result was same with it only using one of the colors.

ColorStateList createCheckedColorStateListComplex(int checkedColor, int offColor) {
         return new ColorStateList(new int[][]{
                 new int[]{android.R.attr.state_checked, android.R.attr.state_pressed}
                 ,new int[]{-android.R.attr.state_checked, android.R.attr.state_pressed}
                 ,new int[]{android.R.attr.state_checked}
                 ,new int[]{-android.R.attr.state_checked}
                 }, new int[]{ checkedColor, offColor, checkedColor, offColor}
         );
     }

So is it possible to have two different colors with tinting by passing a ColorStateList with two different colors.




Aucun commentaire:

Enregistrer un commentaire