Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Improving Our Code Dynamically Changing the Background Color

random color code generator gives negative values for colors:

public int getColor(){ // this method I added in mFactBook Random rand = new Random(); int r = rand.nextInt(255); int g = rand.nextInt(255); int b = rand.nextInt(255); int randomColor = Color.rgb(r, g, b); return randomColor; }

I used above code to generate color numbers within listener.... it turns out these values are all negative. Applying in:

kleur = mFactBook.getColor(); // int kleur is declared relativeLayout.setBackgroundColor(kleur); // relativeLayout is known

does not change any color.

3 Answers

Hello,

The negative number you are getting in your example is normal. The int value you obtain from the rgb method contains an "int" in which the first byte is the alpha level, the second is the red color, the third is green, and the fourth is the blue value. Because of how integers are stored in binary form, having a 1 as the first bit will define a negative number, you might want to look up more on two's compliment here. An example from the Color class's documentation is yellow having the value -256 (0xffffff00).

Well, right where you use the color constructor to store the new color, or:

int randomColor = Color.rgb(r, g, b);

A few things go wrong. Starting from the right, I'm not sure what method you're calling on the Color class, so far as I can tell "rgb" doesn't exist as a method. If you're trying to create a Color object, you should create a new Color(r,g,b), but that's going to give you a color object, so randomColor would be a variable of class Color, not primitive int. All together, this line should look like:

Color randomColor = new Color(r, g, b);

though this will leave you with a Color value to return, not an int like setBackgroundColor requires, you can use randomColor.getRBG() to get the int needed, I believe. setBackgroundColor wasn't really clear what color standard it was using, but I think that should work. Let me know if that works for you!

Nicolas

It looks like the Color class's static methods are being used based on this documentation.

Thanks both James and Nicolas. In deed there is no (r,g,b) method. I overlooked something from the stackoverflow example I used to review this aspect. And I did forget about the 2-complements representation. Of course this results in a negative number in this case.