1

Why CustomButton results grey color? In R.color.blue and green and red are really red n blue and red. I'm trying to random colors of buttons within the colors red blue green from colors.xml.

public void CustomButton(int btnId) {
    Button btn = (Button) findViewById(btnId);

    int[] btnColor = { R.color.blue, R.color.green, R.color.red };
    Random random = new Random();
    int c = btnColor[random.nextInt(btnColor.length)];
    btn.setBackgroundColor(c);

}
Eoop Xoeno
  • 199
  • 1
  • 9
  • I'm not exactly sure what you are trying to do, but if you want to randomize the button colors I think this might help you --> http://stackoverflow.com/questions/6185931/how-to-set-button-color – hanut Apr 08 '13 at 17:24

2 Answers2

1

Try this

you should use

getResources().getColor(yourcolorid) to get the color

Small change in your code

Change this

 btn.setBackgroundColor(c);

to this

 btn.setBackgroundColor(getResources().getColor(c));
Pragnani
  • 20,075
  • 6
  • 49
  • 74
0

Unless you're using custom colors, try removing the 'r':

public void CustomButton(int btnId) {
    Button btn = (Button) findViewById(btnId);

    int[] btnColor = {Color.BLUE, Color.GREEN, Color.RED};
    Random random = new Random();
    int c = btnColor[random.nextInt(btnColor.length)];
    btn.setBackgroundColor(c);

}

This would use the built-in Android class Color.

Tim C
  • 635
  • 5
  • 18
  • I don't want to use Color.BLUE Color.GREEN Color.ORANGE i'm trying to call colors from colors.xml by randomming. Please.. solve it.please – Eoop Xoeno Apr 08 '13 at 17:15