-1

Am I doing anything wrong ? I need to pass the integer variables that I have to the switch.

This works (with numbers):

@BindColor(R.color.white) protected int white;
@BindColor(R.color.black) protected int black;

Passing int value as number

setTextColor(1);

Then, treat at switch:

private void setTextColor(int color){
        switch (color){
            case 1 : {
                textViewUserName.setTextColor(black);
                textViewCardNumber.setTextColor(black);
                break;
            }
            case 2 : {
                textViewUserName.setTextColor(white);
                textViewCardNumber.setTextColor(white);
                break;
            }
        }
    }

But when I pass the int white or black value, the switch doesn't work. Why?

setTextColor(white);

Now switch the id's

private void setTextColor(int color){
        switch (color){
            case R.color.black : {
                textViewUserName.setTextColor(black);
                textViewCardNumber.setTextColor(black);
                break;
            }
            case R.color.white: {
                textViewUserName.setTextColor(white);
                textViewCardNumber.setTextColor(white);
                break;
            }
        }
    }

Nothing happens, no changes at textView color.

Marcello Câmara
  • 187
  • 1
  • 13

2 Answers2

0

I solved my problem by stopping using butterknife and creating a class to get the desired color

public static int getColor(Context context, int color) {
    switch (color){
        case 1 : {
            return (context.getResources().getColor(R.color.yellow));
        }
        case 2 : {
            return (context.getResources().getColor(R.color.purple));
        }
        case 3 : {
            return (context.getResources().getColor(R.color.green));
        }
        case 4 : {
            return (context.getResources().getColor(R.color.grey));
        }
        case 5 : {
            return (context.getResources().getColor(R.color.red));
        }
        default : {
            return 0;
        }
    }
}

Finally, I just need to set the color like this:

imageView.setColorFilter( ClassHelperCreated.getColor(this, color) );
Marcello Câmara
  • 187
  • 1
  • 13
0

You are just mixing up 2 completely different things : colors and ids.

R.color.black is the id of a color you created in a resource file like this #ff000000

black as resolved by your BindColor (or getResources().getColor(R.color.black) in your own response) is the integer with the value equal to 0xff000000 which is -16777216

So in your second switch you are passing a color and compare it with ids and then it's expected that you don't enter any cases of the switch.

By the way this switch is completely unnecessary as all you were doing inside is using the value twice (just use color), but if you really wanted to use a switch you should use black and white as cases and not R.id.white and R.id.black given the input you provided to your function

Xavier Falempin
  • 1,186
  • 7
  • 19
  • That's it. I got it. But, the switch is necessary because I'm saving the "color" choice of user at database (value = 1 or 2, or 3... etc). I've posted my solution at this post. But thank you to tell me what I was doing wrong. – Marcello Câmara Mar 12 '19 at 17:08