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

Java

Color not changing...

Here's the code in my main activity java file:

package com.example.seanmcmanus.artofpeace;

import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity { private QuoteBook mQuoteBook = new QuoteBook(); //declare our view variables private TextView mQuoteTestView; private Button mshowquoteButton; private RelativeLayout mRelativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //assign views from layout file to corresponding variables
    mQuoteTestView = (TextView) findViewById(R.id.quotetextView);
    mshowquoteButton = (Button) findViewById(R.id.showquotebutton);
    mRelativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String quote = mQuoteBook.getFact();
            //update the screen with a dynamic quote
        mQuoteTestView.setText(quote);
            //this is meant to change the color on click, but it does not...not sure why...
        RelativeLayout.setBackgroundColor(Color.RED);
        }
    };



    mshowquoteButton.setOnClickListener(listener);
}

}


and here's the code in the Res xml file:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/relativeLayout" tools:context="com.example.seanmcmanus.artofpeace.MainActivity" android:background="@android:color/background_dark">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quote of the Day"
    android:textSize="20sp"
    android:textColor="@android:color/background_light" />

<TextView
    android:text="Loyalty and devotion lead to bravery. Bravery leads to the spirit of self-sacrifice. The spirit of self-sacrifice creates trust in the power of love. "
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/quotetextView"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/FactTextView"
    android:layout_toEndOf="@+id/FactTextView"
    android:fontFamily="sans-serif-condensed"
    android:textSize="24sp"
    android:textColor="@android:color/background_light" />

<Button
    android:text="Enlighten me"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/showquotebutton"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>


Can anybody explain to me why the color does not change. What have I done wrong here?

2 Answers

RelativeLayout.setBackgroundColor(Color.RED);

It appears you are trying to set the background color of 'RelativeLayout'. Generally, this is done by setting the background color of the variable, not the layout type.

Thankfully, you already declared the variable.

    mRelativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);

So all you do is change the first code I quoted.

mRelativeLayout.setBackgroundColor(Color.RED);

If this doesn't work, let me know. I never plugged it into my IDE as this stuck out.

Philip, thank you!