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 trialAditya Pore
1,453 PointsRandom Generation Design Issue
Hi there, Since we are using random number generation logic for choosing among background colors and textColor for showFactButton, we encounter certain cases wherein the BackgroundColor differs from that of TextColor of the showFactButton. Is there a possible workaround for this? Thanks, Aditya
2 Answers
Steve Hunter
57,712 PointsHI Aditya,
I don't have a working version of this in Android, but do have the iOS Swift version. But, looking at the video here (I've paused it at 5m49s), there are three key lines of code that perform this functionality.
The getColor()
method is called inside the onClick()
method so that this code runs when the button is tapped.
The instance of the ColorWheel
class, mColorWheel
is used to pull back a colour, which is stored in int color
using the following line:
int color = mColorWheel.getColor();
So, for that one click/tap, we now have one colour stored inside a variable. That variable is then used in the two places the colour needs to be amended. These lines achieve that:
relativeLayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
Both properties are set using the same color
variable retrieved from the mColorWheel
instance. No other accessing methods are called to mColorWheel
- the getColor()
method is called once, the returned value assigned to the color
variable and that variable used to change the colours of the button text and the layout background in the two lines above.
Does that make sense?
Steve.
Steve Hunter
57,712 PointsHi Aditya,
I thought that the two colours were in sync.
The method getColor()
is called which returns a color from the ColorWheel
class. This method call happens when the button is clicked each time. The returned colour is assigned to both the button text & background at the same time. There should be no way of the colours being different?
That's how I understood the app's function - maybe that all gets tied up in a later video.
Steve.
Aditya Pore
1,453 PointsDuring the first call to getColor() method, the randomNumber variable is assigned a random number between 0 to 12 and we return the same.
During the second call to getColor() method in FunFactsActivity.java , the randomNumber variable is again assigned a random number between 0 to 12 and we return the same.
Hence I feel that we end up getting two different number's!
Aditya Pore
1,453 PointsAditya Pore
1,453 Pointsyeah !! thanks a lot steve .. I was calling /referring to the color in a different manner:
relativeLayout.setBackgroundColor(mColorWheel.getColor()); showFactButton.setTextColor(mColorWheel.getColor());
Sorry for the confusion and thanks a lot for the code snippet! appreciated :)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsAh right, yes. If you do it that way, you'll get mismatched colours, that's for sure!