Slim C.
Slim C.

Reputation: 1127

Set Min and Max Value for Int? Android

Is there a way to set minimum and maximum value of an integer? I did some research but I couldn't find anything useful. I need to set min value 0 and max value 30, so when user is touching button(TouchListener) numbers will go from 30 to 0. I know it can be done with if else statements but I am looking for a better way.

     int total= Integer.MAX_VALUE + 30;
     int left= Integer.MIN_VALUE + 0;

        total= 30;
        left = 30;
            .....
    onTouchListener...
    switch (action) {
                case MotionEvent.ACTION_DOWN:

    left--;
    setText();
break;
    ...
protected void setText() {
    count.setText(left + "/" + total);
}

But the problem is, int value goes under 0. -1 -2 etc etc How can I stop this? How to set Min int value?

Upvotes: 0

Views: 4304

Answers (3)

Ajinkya Pote
Ajinkya Pote

Reputation: 11

You can use the range seek bar which allow you to seek between two specific integer values.

Here is the link - https://code.google.com/p/range-seek-bar/

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try to check left value before decrees by 1 :

if(left < 0){
   left--;
   setText();
}

Upvotes: 0

aga
aga

Reputation: 29416

Replace left--; with the following:

left -= (left > 0 ? 1 : 0);

Upvotes: 1

Related Questions