2

I was working on a game on Android studio where user touch drives tiles to combine. To do this, I have used a GridLayout separated into rows and columns. The value is displayed with imageview and all gridlayout cells are linked to a separate cell class which includes imageview ID's. I implemented a listener on each image view to detect when a swiping motion occured. The code for initalizing the board and listeners is below:

for(int i = 0; i<noOfBlocks; i++) {
    for (int j = 0; j < noOfBlocks; j++) {
        ImageView imageView = new ImageView(this);
        imageView.setId(iDcnt);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(widthOfBlock, widthOfBlock));
        imageView.setMaxHeight(widthOfBlock);
        imageView.setMaxWidth(widthOfBlock);
        int randomNum = (int) Math.floor(random.nextDouble() * gamepiece.length);
        imageView.setImageResource(gamepiece[randomNum]);
        Cell c = new Cell(i + 1, j+1, randomNum, imageView.getId());
        imageView.setOnTouchListener(new View.OnTouchListener() {
                                         @Override
                                         public boolean onTouch(View v, MotionEvent event) {
                                             SquarePlay s = null;
                                             try {
                                                 s = new SquarePlay();
                                             } catch (ClassNotFoundException e) {
                                                 e.printStackTrace();
                                             } catch (NoSuchMethodException e) {
                                                 e.printStackTrace();
                                             }
                                             s.onSwipeEvent(event);
                                             return false;
                                         }
                                     });
                gameBoard.addView(imageView);
        iDcnt++;
    }

The OnTouch events are handled in a separate class in the method which is shown below:

    float x1, x2, y1, y2, dx, dy;

public void onSwipeEvent(MotionEvent event){
switch(event.getAction()) {
    case(MotionEvent.ACTION_DOWN):
        x1 = event.getX();
        y1 = event.getY();
        break;

    case(MotionEvent.ACTION_UP): {
        x2 = event.getX();
        y2 = event.getY();
        dx = x2-x1;
        dy = y2-y1;

        if(Math.abs(dx) > Math.abs(dy)) {
            if(dx>0)
                onSwipeRight();
            else
                onSwipeLeft();
        } else {
            if(dy>0)
                onSwipeDown();
            else
                onSwipeUp();
        }
    }
}

}

private void onSwipeLeft() {
    System.exit(0);
}

private void onSwipeRight() {
    System.exit(0);
}

private void onSwipeUp() {
    System.exit(0);
}

private void onSwipeDown() {
    System.exit(0);
}

NOTE:System.exit is just there to test if this works.

The App loads but does not produce a response from the swipe events, Does anyone have any suggestions on how to resolve this issue?

Thank you :)

1 Answers1

1

To use swipe You can create your class inside the activity class as follow##

declare the class name inside the activity class

    SwipeListener swipeListener;

inside the activity class write this code to handle the swipe##

private class SwipeListener implements View.OnTouchListener {
    //create gesture detector variable
    GestureDetector gestureDetector;

    //create constructor
    SwipeListener(View view){
        int threshold = 100;
        int velocity_threshold = 100;
        GestureDetector.SimpleOnGestureListener listener =
                new GestureDetector.SimpleOnGestureListener(){
                    @Override
                    public boolean onDown(MotionEvent e) {
                        return super.onDown(e);
                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        //get x and y difference
                        float xDiff = e2.getX() - e1.getX();
                        float yDiff = e2.getY() - e1.getY();

                        try {
                            if(Math.abs(xDiff)>Math.abs(yDiff)){
                                if(Math.abs(xDiff)>threshold &&
                                        Math.abs(velocityX)> velocity_threshold){
                                    if(xDiff > 0){
                                        //swipe right
                                        Intent intent = new Intent(TopUpBalance.this, HomePage.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }else{
                                        //swipe left
                                    }
                                    return true;
                                }//you can handle swipe down and above here as well, the same way above
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        return false;
                    }
                };
        //init gesture

        gestureDetector = new GestureDetector(listener);

        view.setOnTouchListener(this);



    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }
}

Declare your SwipeListener onCreate method as follow##

  linearLayout = findViewById(R.id.layoutID);

    //swipe
    swipeListener = new SwipeListener(linearLayout);