mercredi 18 décembre 2019

how to set the "Color.rgb" in Java to random?

I'm experimenting with Android studio and I wanted to pass a random value to the color.rgb. This is the code that I tried

int x  = rand.nextInt(9);
int y  = rand.nextInt(9);
int z  = rand.nextInt(9);

viewTest.setBackgroundColor(Color.rgb(x,y,z));

edit: the color range is between 0 and 255 so to right code is

int x  = rand.nextInt(255);
int y  = rand.nextInt(255);
int z  = rand.nextInt(255);

viewTest.setBackgroundColor(Color.rgb(x,y,z));

not the best solution but it works Thanks to jon Skeet for remembering me :)




Aucun commentaire:

Enregistrer un commentaire