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

Do nested if statements work?

Hi

I am making a simple "colour mix" app. 2 buttons appear at the top of the screen. When clicked, a new primary colour is shown. There is a text field below these buttons which should display the secondary colour as applicable. This is the bit thats not currently working. I wonder if I have it in the correct place?

public class ColourMix extends Activity
{
    int number = 0;

    String[] colours =
            {
            //red
            "#4E62B5",
            //yellow
            "#EB233E",
            //blue
            "#D9E319"
            };

    String colour = "";

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

        final Button top = (Button) findViewById(R.id.top);
        final Button middle = (Button) findViewById(R.id.middle);
        final TextView bottom = (TextView) findViewById(R.id.bottom);

        View.OnClickListener topListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                number++;
                if (number == 3)
                {
                    number = 0;
                }

                String numberAsString = number + "";
                top.setText(numberAsString);

                if (numberAsString.equals("0"))
                {
                    colour = colours[number];
                    int colourAsInt = Color.parseColor(colour);
                    top.setBackgroundColor(colourAsInt);
                    top.setTextColor(colourAsInt);

                } else if (numberAsString.equals("1"))
                {
                    colour = colours[number];
                    int colourAsInt = Color.parseColor(colour);
                    top.setBackgroundColor(colourAsInt);
                    top.setTextColor(colourAsInt);

                } else {colour = colours[number];
                    int colourAsInt = Color.parseColor(colour);
                    top.setBackgroundColor(colourAsInt);
                    top.setTextColor(colourAsInt);
                }
            }
        };

        top.setOnClickListener(topListener);

        View.OnClickListener bottomListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                number++;
                if (number == 3)
                {
                    number = 0;
                }

                String numberAsString = number + "";
                middle.setText(numberAsString);

                if (numberAsString.equals("0"))
                {
                    colour = colours[number];
                    int colourAsInt = Color.parseColor(colour);
                    middle.setBackgroundColor(colourAsInt);
                    middle.setTextColor(colourAsInt);
                }
                else if (numberAsString.equals("1"))
                {
                    colour = colours[number];
                    int colourAsInt = Color.parseColor(colour);
                    middle.setBackgroundColor(colourAsInt);
                    middle.setTextColor(colourAsInt);
                }
                else {colour = colours[number];
                    int colourAsInt = Color.parseColor(colour);
                    middle.setBackgroundColor(colourAsInt);
                    middle.setTextColor(colourAsInt);


                    if (top.equals("1") && middle.equals("0"))
                    {
                        String orange = "#F7830F";
                        int orangeAsInt = Color.parseColor(orange);
                        bottom.setBackgroundColor(orangeAsInt);
                    }
                }
            }
        };

        middle.setOnClickListener(bottomListener);
  }

}

1 Answer

So I see a few potential things here causing you trouble. As a general note, yes, nested if statements do work but you need to be careful how you use them and in making sure the nested part of the statement is where it fits logically.

Potential things that are giving you grief:

  • In your current nested if statment, you have:
if (top.equals("1") && middle.equals("0")) {...}

The top and middle objects are not strings; rather, they are buttons. So instead of checking if top.equals("1"), you probably want something like top.getText().toString().equals("1") instead. The top.getText().toString() allows you to get at the text in the button rather than the button object.

  • Your current nested if statement will never be evaluated even as written with no.1's changes taken into account:
else {
                   ...

                    if (top.equals("1") && middle.equals("0")){...}

}

At the point when you reach the else statement, we know that middle's text is 2, as numberAsString is not 0 and is not 1. Therefore the check inside of that else statement to see if the text of the middle button object is 0 will never be true. You could alternatively try something like this to get at the case when the text in the top button is 1 and the text in the middle button is 0:

if (numberAsString.equals("0"))
{
    // the code you already have...

    // WE KNOW THAT MIDDLE's TEXT IS 0
   // SO LET's CHECK ON TOP's TEXT:
   if (top.getText().toString().equals("1")
    {
          //Middle's Text is 0 and Top's text is 1.
    }

}
else if (numberAsString.equals("1"))
{
    // the code you already have...
}
else
{
    // the code you already have...
   // without the nested if statement.

}

Also, I think from your description that you will eventually likely want to check for the case when middle's text is 0 and top's text is 1 from both middle and top's click listeners.

Hope this helps!

Hi Rebecca

Thank you for the advice - really helpful examples. That works perfectly!

I have attempted creating a new method and called that from within the onClick listener and thats working well too.

Many thanks

Holly