3

I have defined a color attr, set it in a style and I am using the below code to use the value. Everything is working fine as long the color is different from #ffffffff (means according to the code below, for any color defined in the style the color variable is getting the correct value, but for white the value in color is -1). It seems that with my code the color value must be smaller than #ffffffff and I don't understand why.

this is the attr definition:
<attr name="viewLvActive" format="color"/>

this is the line in the custom style:
<item name="@attr/viewLvActive">#ffffffff</item>

This is the code, which I use to get this color value from my theme:

Resources.Theme theme = mCtx.getTheme();
TypedValue styleID = new TypedValue();
if (theme.resolveAttribute(R.attr.viewLvActive, styleID, true))
    color=styleID.data;
else
    color=-1;

Finally I can live with a color of #fffffffe but still I would like to know where is the mistake.

KarlKarlsom
  • 5,868
  • 4
  • 29
  • 36

1 Answers1

0

What is the type of the color variable? Because if it's int, then -1 == #ffffffff. If you're checking something like:

if (color == -1) {

you'll get true for #ffffffff. An int is 32 bits and an ARGB color will fill all of it up. You can't use a value like -1 (or any other value) to mean "no color".

Lawrence Kesteloot
  • 4,149
  • 2
  • 31
  • 28