0

I want the output to be green if the counter is 20 or above and red if it is below 20. this is the code I have so far and it isnt working.

 add1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub counter = counter += 1; if (counter >= 20) { display.setText(Color.GREEN); //display.setText("" + counter); } else if (counter < 20) { display.setTextColor(Color.RED); //display.setText("" + counter); } display.setText("" + counter); } }); sub1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub counter = counter -= 1; if (counter >= 20) { display.setText(Color.GREEN); //display.setText("" + counter); } else if (counter < 20){ display.setTextColor(Color.RED); //display.setText("" + counter); } display.setText("" + counter); } }); 
2
  • Did you mean: display.setTextColor(Color.GREEN); instead of display.setText(Color.GREEN);? Commented Oct 12, 2013 at 22:16
  • poor if/else construct Commented Oct 12, 2013 at 22:18

3 Answers 3

2

Couple of issues with your code:

  • use counter += 1 instead of counter = counter += 1. Same for subtraction.
  • avoid duplicating code
  • use setTextColor() instead of setText().

add1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { counter += 1; updateDisplay(); } }); sub1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { counter -= 1; updateDisplay(); } }); 

And have this method somewhere:

void updateDisplay () { display.setTextColor(counter < 20 ? Color.RED : Color.GREEN); display.setText("" + counter); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Well put, I missed the counter issue on my first read through.
@Peter - I missed it as well at first.
Thank you to everyone for your time and input. I am very very new at this and trying to teach myself through several tutorials without abusing others knowledge on these kind of forums. I really appreciate the help
0

If you're trying to change the color of the textview called display you need to switch:

display.setText(Color.GREEN); 

to

display.setTextColor(Color.GREEN); 

Also you can probably make your else if just an else.

Comments

0

Well,

As mentioned in the comments, you are trying to set the text to Color.GREEN which is a constant integer representing the green color, while you should be doing like you did in the RED case

display.setTextColor(Color.GREEN); 

On a different matter, You shouldn't be using else if in that statement.

Since your first if asks, is my counter 20 or more, change color to green, and if it's under, change to red.

You only have 2 cases which overlaps the entire "spectrum" of your variable if you will.

You should use if and else in that case

 counter = counter += 1; if (counter >= 20) { display.setTextColor(Color.GREEN); //display.setText("" + counter); } else { // not 20 or above, meaning < 20 display.setTextColor(Color.RED); //display.setText("" + counter); } display.setText("" + counter); 

else if would be useful if you had more cases, for example

 counter = counter += 1; if (counter >= 20) { display.setText(Color.GREEN); //display.setText("" + counter); } else if (counter < 20 && counter >= 10) { // counter is between 10 and 20, not including 20 display.setTextColor(Color.RED); //display.setText("" + counter); } else { // under 10, not including 10 display.setTextColor(Color.WHITE); //display.setText("" + counter); display.setText("" + counter); 

It might be good practice to try and always have an else statement to check for an illegal value (especially if it's an input from the user). even only for a sanity check, or error handling and printing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.