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

Colour Wheel is not changing to other colours only red

I have gone through this tutorial over and over again and I still keep getting this problem with the colour wheel. When I run the app and I click on "show another fun fact" the next fact is displayed with red background only but not with a different background colour. What am I doing wrong ?

Code is below

FUNFACTACTIVITY.JAVA

package com.example.curtis.funfacts;

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

public class FunFactsActivity extends AppCompatActivity { private FactBook mFactBook = new FactBook(); private ColorWheel mColorWheel = new ColorWheel();

//Declare our View variables
private TextView mFactTextView;
private Button mShowFactButton;
private RelativeLayout mRelativeLayout;


private ColorWheel mColorWheel = new ColorWheel();

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

    //Assign the views from the layout file to the corresponding variables
    mFactTextView = (TextView)findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.showFactButton);
    mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);


    View.OnClickListener Listener = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            //update the label with our dynamic fact
            String fact = mFactBook.getFact();
            int color = mColorWheel.getColor();

            // update the screen with our dynamic fact
            mFactTextView.setText(fact);
            mRelativeLayout.setBackgroundColor(Color.RED);

        }
    };
    mShowFactButton.setOnClickListener(Listener);

}

}

COLORWHEEL.JAVA package com.example.curtis.funfacts.test;

    import android.graphics.Color;

    import java.util.Random;

/**

  • Created by Curtis on 23/11/2015. */ public class ColorWheel {

    // member variable ( properties about the object ) public String[] mColors= { "#39add1", "#3079ab", "#c25975", "#e15258", "#f9845b", "#838cc7", "#7d669e", "#53bbb4", "#51b46d", "#e0ab18", "#637a91", "#f092b0", "#b7c0c7" }; // method (abilities : things the object can do ) public int getColor(){ //update fct label with new fact String color = "";

    // randomly select a fact
    Random randomGenerator = new Random();//construct a new random number generator
    int randomNumber = randomGenerator.nextInt(mColors.length);
    //convert random number to text fact
    color = mColors[randomNumber];
    int colorAsInt = Color.parseColor(color);
    
    return colorAsInt;
    

    } }

1 Answer

Landen Melton
Landen Melton
4,204 Points
View.OnClickListener Listener = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            //update the label with our dynamic fact
            String fact = mFactBook.getFact();
            int color = mColorWheel.getColor();

            // update the screen with our dynamic fact
            mFactTextView.setText(fact);
            cmRelativeLayout.setBackgroundColor(Color.RED);

        }
    };

In your click listener you're getting a random color with int color = mColorWheel.getColor(); but you're always setting the background color to red with mRelativeLayout.setBackgroundColor(Color.RED);

I'm assuming you need to do something like mRelativeLayout.setBackgroundColor(color);