android change images using left and right swipe

I am making an android app and whenever the app runs, an activity screen with several components comes up. Now, this activity has several different components. One of these is an image view which shows different images from the gallery. Currently these images change by a touch gesture. But I want to implement the same using left and right swipe. Here is my code:

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;


import java.util.Collection;

public class pictures extends ContentResolverAnimatedViewContainer 
{
    public static final String IMAGE_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
    public static final String IMAGE_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);
    public static String getBucketId(String path) 
    {
        return String.valueOf(path.toLowerCase().hashCode());   }
    static Context cc;
    String cp;
    int picheight;

    public PhotoExample(final Context context) {
        super(context, 9);
        cc = context;
        picheight = getResources().getDimensionPixelSize(R.dimen.photo_example_height);
        final String[] projection = { Images.Media.DATA };
        final String selection = Images.Media.BUCKET_ID + " = ?";
        final String[] selectionArgs = { IMAGE_ID };
        createContentResolverLoop(Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);   }
    @Override
    public void onCreateData(ContentResolver contentResolver, Cursor dataCursor) {
        final int column_data = dataCursor.getColumnIndexOrThrow(Images.Media.DATA);
        cp = dataCursor.getString(column_data); }

    @Override
    public void onCreateViewContent(LayoutInflater layoutInflater, ViewGroup parentGroup, View[] containerViews, int index) {
        containerViews[index] = layoutInflater.inflate(R.layout.example_photo, parentGroup, false);
        ImageView photoView = (ImageView)containerViews[index].findViewById(R.id.photo);
        photoView.setImageBitmap(decodeSampledBitmapFromFile(cp, 0, picheight));    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int Width, int Height) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.Size = calculateInSampleSize(options, Width, Height);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);}

    public static int calculateInSampleSize(BitmapFactory.Options options, int Width, int Height) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int Size = 1;

        if (height > reqHeight || width > reqWidth) {
            final int height_half = height / 2;
            final int width_half = width / 2;

            while ((height_half / Size) > Height && (width_half / Size) > Width) {
                Size = Size * 2;
            }    }

        return Size;
    }


    @Override
    public void cleanup() { }

}

Any ideas how should I implement thus?

Thanks.

Use one of the many Tinder-like swippable cards libraries. DuckDuckGo is your friend (or Google).

Some examples:

  • https://github.com/kikoso/Swipeable-Cards
  • https://github.com/Diolor/Swipecards

You have plenty of options to choose.

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } 
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}
}

Then to use:

yourView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeTop() {
    Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
    Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
    Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
    Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}
});