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

How do you increment a number?

I'm in the beginnings of a simple app. I have a button and I want to increment a number and show that number on the button when pressed. I also want to loop the numbers to only show 0,1,2.... but I'm still on the basics of just getting the increment to work!

This is my code so far but every time I try to run it it crashes the Emulator saying "unfortunately, ColourMix has stopped working".

I've tried cleaning and re-building. I also deleted a lot more code to try and understand the problem. I can't see what I'm doing wrong. Please could some one shed some light?

public class ColourMix extends Activity {

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

//link up the button with the layout 
        final Button top = (Button) findViewById(R.id.top);

         //start the number at zero
         final int number = 0;

         //onClick Listener to be assigned to the top Button
         View.OnClickListener topListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int NewNumber = number + 1;
                top.setText(NewNumber);
            }
        };

         top.setOnClickListener(topListener);

        }
    }

private int number=0;

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

    final Button top = (Button) findViewById(R.id.top);

    top.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            number++;
            String numberAsString = number+"";
            top.setText(numberAsString);
        }
    });
}

Hi Holly,

Why not try it like this.

Remember you can't declare a variable as final if you want to change its value. Also when you setText the variable needs to be a string. Therefore you need to convert your number into a String.

Cheers

Luke

3 Answers

Luke's comment is great. Make sure to set the initial text on the button to 0 in the onCreate method if you want it to initially show up with zero.

Do you want to loop the number based on clicks as in 0,1,2,0,1,2,0,1,2,...? If so, you can change:

String numberAsString = number+"";

to

String numberAsString = (number%3)+"";

The modulo operation (%) can be used to find the remainder of a division, in this case the remainder when your number when divided by 3, so for example:

0%3 = Remainder of 0/3 which is 0, 
1%3 = Remainder of 1/3 which is 1, 
2%3 = Remainder of 2/3 which is 2, 
3%3 = Remainder of 3/3 which is 0 (as 3 is perfectly divisible by itself), 
4%3 = Remainder of 4/3 which is 1, 
etc... 

This is one of a few ways of doing that. Another option would be resetting the number to 0 whenever it reaches 3, so for example:

top.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            number++;
            if (number == 3) {
                  number = 0;
            }
            String numberAsString = number+"";
            top.setText(numberAsString);
        }
    });

If, on the other hand, you just want to limit the number on the button to being a max count of 2 (0,1,2,2,2,2,2,...), you can try:

top.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (number < 2) {
                 number++;
            }
            String numberAsString = number+"";
            top.setText(numberAsString);
        }
    });

Thanks Luke and Rebecca. Great clear examples of how this can be done.

Thank you for your help :)

What happened to good old-fashioned

variableName++

?

Thanks Luke!

I set it to final as android made me when I wrote the onClickListener method and if it wasn't final an error occurred. I've moved the int to outside the onCreate.

The emulator is working now, happy days!

Just one more question.... how do I now limit the number to 2?

Holly