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

Nathan Bird
Nathan Bird
2,189 Points

Using .setBackground() in android studio

I have this line of code:

winner.setBackground();

but what exactly am I supposed to put in the parentheses?

1 Answer

Pushparaj Samant
Pushparaj Samant
4,607 Points

You are supposed to specify the color you want to set to the background. It should be a integer value so if you have HEX value convert it to integer using Color.parseColor method to convert it to integer and then set inside those paranthesis

Nathan Bird
Nathan Bird
2,189 Points

Yes, but in android studio auto complete gives me this:

image.setBackground(Drawable background)
Pushparaj Samant
Pushparaj Samant
4,607 Points

Thats alright. Just delete that and insert your integer value and try.

Nathan Bird
Nathan Bird
2,189 Points

I'm trying to set a background image not a background color.

winner.setBackground(Color.WHITE); // This  does not work because .setBackground() is asking for a drawable image!
winner.setBackgroundColor(Color.WHITE); // this is the way to set a Background Color
Nathan Bird
Nathan Bird
2,189 Points
pathName = "/drawable/img1"
winner.setBackground(Drawable.createFromPath(pathName));

Only problem now is the path name! The Java file and the activity.xml file are in completely different folders!