4

I'm writing an app which changes the background color of the activity each time you press the button. And this is what I have till now. But it is not working! What am I doing wrong?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button) findViewById(R.id.button1);        
    final View a = findViewById(R.id.m);        
    final Random color = new Random();
    final Paint p = new Paint();


    b.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {

             p.setARGB(256,color.nextInt(256),color.nextInt(256),color.nextInt(256));                  
        a.setBackgroundColor((p.getColor()));

        }
    });
}

It's working when I pass a single color, for example a.setBackgroundColor(Color.GREEN);

Uday Kanth
  • 361
  • 3
  • 9
  • 16
  • what does it do when you press the button? – FoamyGuy Jul 19 '12 at 18:38
  • @Tim Nothing. The screen stays white. – Uday Kanth Jul 19 '12 at 19:09
  • 1
    Have you tried using the `Color` class? Like so: `a.setBackgroundColor(Color.argb(256, color.nextInt(256), color.nextInt(256), color.nextInt(256)));` – Cat Jul 19 '12 at 19:14
  • make 3 separate int items for each of your colors and set them with random values first then output them to the log before trying to set them as the background. that will help to narrow down the problem. – FoamyGuy Jul 19 '12 at 19:20
  • there is a typo: `p.setARGB(256`, should be `p.setARGB(255` – Maxime Sep 29 '15 at 11:20

5 Answers5

4

It's probably too late but i was looking for the same thing and as i read this thread i came up with the solution of the problem.

You are using 256 for alpha and also 256 for max random. but values to use are 0-255. if u change that it will work well.

Random color = new Random();
a.setBackgroundColor(Color.argb(255, color.nextInt(255), color.nextInt(255), color.nextInt(255)));

Cheers

Memme
  • 178
  • 11
2

I'm not sure if this will work (but it's worth a try):

Try initializing color = new Random() within the onClick() statement.

b.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {
         color = new Random();
         p.setARGB(256,color.nextInt(256),color.nextInt(256),color.nextInt(256));                  
    a.setBackgroundColor((p.getColor()));

    }
});

Also, look at this question:

Android: Generate random color on click?

it seems like it's trying to accomplish a similar goal.

Community
  • 1
  • 1
Kgrover
  • 2,106
  • 2
  • 36
  • 54
  • 1
    Well I actually solved it by randomizing the alpha value as well. But I have no idea why passing the value 256 didn't work! Anyway, thanks for the effort. :) – Uday Kanth Jul 19 '12 at 23:46
  • Ah, That's weird. Well, if I helped at all, you can always accept or upvote ;) – Kgrover Jul 21 '12 at 03:59
  • 1
    @UdayKanth - you can't pass 256 because the highest value it will accept is 255. You have eight bits to work with, which means that you have 256 *numbers*, but it starts at 0, and ends at 255. – Phill.Zitt Sep 24 '12 at 17:15
  • use 1 random instead of 3 ------------- int color = 0xFF000000 | mRandom.nextInt(0xFFFFFF); – Umesh Chhabra Jun 01 '16 at 16:49
2

For random color I wrote a method (you need import android.graphics.Color; import java.util.Random;):

int randomColor() {
    Random r = new Random();
    int red = r.nextInt(256);
    int green = r.nextInt(256);
    int blue = r.nextInt(256);
    return Color.rgb(red, green, blue);
}

Then I just use it like this:

Paint p = new Paint();
p.setColor(randomColor());
Andrew
  • 36,676
  • 11
  • 141
  • 113
0

It look's like you are on the correct path. Don't forget to get your seed or else you will be getting the same "random" values everytime.

Random color = new Random(System.currentTimeMillis());

Try wrapping it up in a post.

view.post(new Runnable() {
      @Override
      public void run() {
          // setbackground here
      }
}
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
-1
p.setARGB(200,color.nextInt(256),color.nextInt(256),color.nextInt(256));                  
a.setBackgroundColor((p.getColor()));
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84