Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colors in Android

I am using colors in Android but I don't know the format. Here's a working example in JAVA.

public Color getColor(int i) {
  switch (i%8) {
     case 0: return Color.blue;
     case 1: return Color.red;
     case 2: return Color.magenta;
     case 3: return Color.orange;
     case 4: return Color.pink;
     case 5: return Color.green;
     case 6: return Color.cyan;
     case 7: return new Color(250, 220, 100);
  }
  return (new Color((i*80)%255,(i*80)%255,(i*80)%255)) ;      

}

How can I create this using Android. What I'm trying to do is return a color of eight possibilities.

like image 592
user3364217 Avatar asked Dec 06 '25 02:12

user3364217


1 Answers

The name of color constants are uppercase on an Android, and a color is an int value. So you should change the return type from Color to int. Second you should change the constants name to reflect Android. Third you should use Color.makeColor to get the color from integer values

public int getColor(int i) {
  switch (i%8) {
     case 0: return Color.BLUE;
     case 1: return Color.red;
     case 2: return Color.magenta;
     case 3: return Color.orange;
     case 4: return Color.pink;
     case 5: return Color.green;
     case 6: return Color.cyan;
     case 7: return Color.makeColor(250, 220, 100);
  }
  return Color.makeColor((i*80)%255,(i*80)%255,(i*80)%255)) ;  
}
like image 184
Blackbelt Avatar answered Dec 08 '25 16:12

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!