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

Joshua Friedman
Joshua Friedman
537 Points

Extra Credit: Learning the Language and changing background color

Beginner here: I'm utterly lost as to how to even start with adding an ID to relative layout. I tried manually entering an ID into the XML file, but I can't get it to organize imports in the same way that I could have with the button and textview.

Are we to add this new ID via the graphical layout function, or are we to enter it by hand?

Daniel Hartin
Daniel Hartin
18,106 Points

in the XML file within the relative layout brackets add the line android:id="@+id/whateverYouWantYourIdToBe". You can then reference this in your java code. What are you trying to achieve though with the relative layout?

2 Answers

Iskandar Fendi
Iskandar Fendi
5,700 Points

Yes, that is right from Daniel Hartin. Just add an ID as usual in XML File. Basically look like this:

<RelativeLayout android:id="@+id/whateverYouWantYourIdToBe" blah blah

<Button> Blah...... </Button>

<TextView> </TextView>

</RelativeLayout>

After you have added an id in XML, You go to your mainActivity.java to do change background behavior.

Somewhere around there (Same territory where you initiate Button and TextView) you do:

RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.theword);

That is how you initiate it. TIPS: WHen you try to type RelativeLayout, don't type it completely use Ctrl + Space (FOr intelleSense). It gives me an error when I type it completely Please let me know if this is what you are looking for. If you need further help of how to do the changing backround. Please let me know.