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 Improving Our Code Adding Colors

Mert Kahraman
Mert Kahraman
12,118 Points

Can't set the textColor of a label

Hi all,

My code for this code challenge is below, and I can't get passed the code challenge. I would appreciate any help!

Thanks, Mert

Code Below:

public class MealActivity extends Activity {

public TextView foodLabel;
public TextView drinkLabel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_meal);

    foodLabel = (TextView) findViewById(R.id.foodTextView);
    foodLabel.setTextColor(Color.BLUE);
    drinkLabel = (TextView) findViewById(R.id.drinkTextView);
    foodLabel.setTextColor(Color.GRAY);
    RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
    mealLayout.setBackgroundColor(Color.GREEN);
}

}

MealActivity.java
public class MealActivity extends Activity {

    public TextView foodLabel;
    public TextView drinkLabel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meal);

        foodLabel = (TextView) findViewById(R.id.foodTextView);
        foodLabel.setTextColor(Color.BLUE);
        drinkLabel = (TextView) findViewById(R.id.drinkTextView);
        foodLabel.setTextColor(Color.GRAY);
        RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
        mealLayout.setBackgroundColor(Color.GREEN);
    }
}

2 Answers

Check your code

foodLabel = (TextView) findViewById(R.id.foodTextView);
foodLabel.setTextColor(Color.BLUE);
drinkLabel = (TextView) findViewById(R.id.drinkTextView);
foodLabel.setTextColor(Color.GRAY);

You are changing the text colour for foodLabel twice. And you are not changing the text colour for drinkLabel at all.

Mert Kahraman
Mert Kahraman
12,118 Points

Yes you are right! It should've been like this: foodLabel = (TextView) findViewById(R.id.foodTextView); foodLabel.setTextColor(Color.BLUE); drinkLabel = (TextView) findViewById(R.id.drinkTextView); drinkLabel.setTextColor(Color.GRAY);

I did it and tried it again. This won't work too! What would you suggest?

You probably have a different error now. Please paste all your code.