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 Creating the Screen Layout Setting Colors

Challenge Task 2 of 2

I don't know to how to use the color or code color

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/#fff092b0" >

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Treehouse loves me!"
    android:textColor="@android:color/white"
    android:textSize="72sp" />

</RelativeLayout>

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

There are a couple ways to show color.

If this is a color that will be used a lot through out your app. You will want to do the following

 android:background="@color/toolbar_background"

This says make background equal to some color that I called toolbar_background.

How does this work? Inside your directory structure for you application. There is a folder called values. It's inside the "res" folder. What you do is create a file called color.xml

Inside that you put

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <color name="toolbar_background">#000000</color>

All together, inside my layout. I have referenced @ color. Which by default will look for a file "color.xml" inside the values folder.

This way whenever I want to use this color, I just reference it like I did above. That way if I want to change the color I can change it in the color.xml in one place. Thus changing it through out the whole app. This is the BEST way to do this.

Android also has default colors that you don't need to add to the color.xml. That is why they reference @adroid for the color since it's an android standard color.

HERE IS THE ANSWER -- when using colors in hex format. You just have to put the color in. See android:Background for an example.

There is also an example of @android:color/white. If you had a color.xml defined, you wouldn't need the @android since @android says it's an android defautl, or that is how I think about it.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#fff092b0" >

  <TextView
    android:id="@+id/textView1"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Treehouse loves me!"
    android:textSize="72sp" />

</RelativeLayout>