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

Condition to check 2 colour matches in if statements

I am making a very simple colour wheel calculator. When 2 primary colours are selected the secondary colour appears.

The colour selection is working but my outcome is not and I think I am not using the condition in the if statement correctly.

Heres all my code (hopefully its not too long for this comment)

Also, I'm not sure if theres a better way to do this as basically i have 2 buttons on the screen (top button & bottom button). I think I need about 9 if statements to do every eventuality ie. if top button is red and bottom button is blue result = purple and so on).

public class MainActivity extends Activity {

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

        // declare the layout buttons
        final Button topButton = (Button) findViewById(R.id.topButton);
        final Button bottomButton = (Button) findViewById(R.id.bottomButton);
        TextView colourResult = (TextView) findViewById(R.id.newColour);

        //declare the colours
        String red = "#ffff1e31";
        String blue = "#FF4F87FF";
        String yellow = "#FFFFF328";
        String orange = "#FFFF8334";
        String green = "FF34FF3A";
        String purple = "#FFAF58FF";


        // Set up colours in an array
        final String[] colours = {
                red,
                blue,
                yellow};

        //Change the colour when the top button is clicked
        View.OnClickListener topButtonClick = new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
               //create random number generator
                Random randomGenerator = new Random();
               //call a random number based on the length of the colours array
                int randomNumber = randomGenerator.nextInt(colours.length);
                //create a new string called colour
               String colour = "";
                //call the HEX colour from the array
               colour = colours[randomNumber];
                //convert the HEX colour into a integer
                int setColour = Color.parseColor(colour);
                //set the background of the button to the colour
               topButton.setBackgroundColor(setColour);
            }
        };

        //Change the colour when the bottom button is clicked
        View.OnClickListener BottomButtonClick = new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                //create random number generator
                Random randomGenerator = new Random();
                //call a random number based on the length of the colours array
                int randomNumber = randomGenerator.nextInt(colours.length);
                //create a new string called colour
                String colour = "";
                //call the HEX colour from the array
                colour = colours[randomNumber];
                //convert the HEX colour into a integer
                int setColour = Color.parseColor(colour);
                //set the background of the button to the colour
                bottomButton.setBackgroundColor(setColour);
            }
        };

        //assign the onclick method to the buttons
        topButton.setOnClickListener(topButtonClick);
        bottomButton.setOnClickListener(BottomButtonClick);

        //change the outcome colour based on the top and bottom buttons
        if  (topButton.equals(red) && bottomButton.equals(yellow)){
            int setResultColour = Color.parseColor(orange);
            colourResult.setBackgroundColor(setResultColour);
        };

Hi,

It appears that you are attempting to evaluate topButton and bottomButton directly as COLORS (but in your code, they actually represent instances of button objects) - so your code is really saying: "If the topButton is the color red" ... what you want to probably test is "if the background color of the topButton is red... "

Also keep in mind that buttons do not have a simple "getBackgroundColor" method - so one way is to use the PaintDrawable object to get the color as an int (and then convert to a hex string) ...

PaintDrawable drawable = (PaintDrawable) topButton.getBackground();
int color = drawable.getPaint().getColor();
String hexColor = String.format("#%06X", (0xFFFFFF & color));

Probably easier to extract all that into a method that takes a button and returns a string...

HTH