user2869238
user2869238

Reputation: 77

Else If android not working

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);
        }
    });

Upvotes: 0

Views: 434

Answers (3)

kamituel
kamituel

Reputation: 35950

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);
}

Upvotes: 2

La bla bla
La bla bla

Reputation: 8708

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.

Upvotes: 0

Peter
Peter

Reputation: 5121

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.

Upvotes: 0

Related Questions