Q:

How to handle swipe in android?

I am creating swipe in my app. When I am on last image and I swipe from right to left. Its still on same image thats fine. Then I swipe again from right to left. And then from left to right my app it crash. Similar case on first image. I know this is because of the increment in variable. I want if user on last image and he swipe from right to left even many times. And then if he swipe from left to right previous image come. Please help me to solve this.
position lies b/w 0 to 8.
Code-
position = i.getExtras().getInt("id");
public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            if(position<mThumbId.length){
                position++;
                 changeBackground();
            }
    }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(position>=0){
                position--;
                changeBackground();
           }
        }

      private void changeBackground(){
        full.setBackgroundResource(mThumbId[position]);
   }

I am getting array index out of bound error length=9 index=9 when I am on last image and swipe from right to left then swipe from left to right.
And I am getting array index out of bound error. length=9 index=-1. When I am on first image and swipe from left to right then swipe right to left.
Edit-
I want if user is on first image and he swipe from left to right image fixed. And when he swipe from right to left next image come.

A:

This should do the trick :) I did not test it but it should work.
public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            if(position < mThumbId.length - 1){
                position++;
                 changeBackground();
            }
    }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(position > 0){
                position--;
                changeBackground();
           }
        }

      private void changeBackground(){
        full.setBackgroundResource(mThumbId[position]);
   }

