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

Jason Holt
Jason Holt
10,041 Points

Change the background color conditionally

I am trying to understand how to complete the second extra credit challenge at the end of Learning the Language. I am very stuck.

The challenge:

Change the background color conditionally Add "if" statements to check the value of the answer in the onClick() method. You can compare >it to the values stored in the mAnswers array of the CrystalBall object using the String.equals() >method. For example, answer.equals(crystalBall.mItems[0]) checks whether the answer equals the first >item in the array or not. Change the background color for certain answers or certain types of >answers, like make it red for negative answers and green for positive answers.

Here is what I have tried so far:

in the MainActivity.java and in CrystalBall.java I have tried to setup an if statement like this:

if (answer.equals(mAnswers.mItems[0])); "mItems cannot be resolved or is not a field"

I don't know where my understanding has broken down but I am missing something.

1 Answer

I need to see the provided code also. Are mAnswers and mItems related (mAnswers.mItems[0] looks fishy)?

Jason Holt
Jason Holt
10,041 Points

CrystalBall.Java

public String[] mAnswers = {
            "It is certain",
            "It is decidedly so",
            "All signs say YES",
            "The stars are not aligned",
            "My reply is no",
            "It is doubtful",
            "Better not tell you now",
            "Concentrate and ask again",
            "Unable to answer now",};

    // Methods (abilities: things the object can do)
    public String getAnAnswer() {

        String answer = "";

        Random randomGenerator = new Random(); 
        int randomNumber = randomGenerator.nextInt(mAnswers.length);

        answer = mAnswers[randomNumber];

        return answer;

and MainActivity.java

public class MainActivity extends Activity {

    private CrystalBall mCrystalBall = new CrystalBall();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Above lines connect us to the XML layout.

        // Declare our View variables and assign them the Views from the layout file
        final TextView answerLabel = (TextView) findViewById(R.id.textView1); 
        Button getAnswerButton = (Button) findViewById(R.id.button1);

        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String answer = mCrystalBall.getAnAnswer();

                // Update the label with our dynamic answer
                answerLabel.setText(answer);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 

I am trying to get the OnClick listener to change the color of the background based on the answer that is displayed from the array in CrystalBall.java

I am just trying to understand what I don't understand, the missing piece or pieces to set this up according to the challenge. My above code does not contain any of the trial code right now.