0

I have the following code:

final float len = getResources().getDisplayMetrics().densityDpi/6;    

linear1.setOnTouchListener(new View.OnTouchListener() {
                    float initX;
                    float initY;
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                initX = event.getX();
                                initY = event.getY();
                                break;
                            case MotionEvent.ACTION_UP:
                                initX -= event.getX();
                                initY -= event.getY();
                                if (initY > len) {
                                    //Logic For Swipe Right
                                } else if(initY < -len) {
                                    //Logic For Swipe Left   
                                }
                                break;
                         }
                         return false;
                    }
    });

I am trying to make it so that it doesn't perform the logic to "swipe right" when I swipe down and so that it doesn't perform the logic to "swipe left" when I swipe up. I only want right and left detection for the "swipe right" logic and "swipe left" logic, respectively. Right now, when I swipe up it is also doing the "swipe right" logic and when I swipe down it is doing the "swipe left" logic.

Any help on how I would alter this code to avoid that? Maybe even have a separate section to add logic for "swipe up" and "swipe down" separately?

Ammer12
  • 13
  • 6
  • Can you try swiping at angles, up & right, up & left, down & right, down & left. What triggers for each case? – Chris DL Feb 26 '19 at 17:39
  • Use this answer maybe it will help you. https://stackoverflow.com/a/938657/9560126 – TheCoderGuy Feb 26 '19 at 17:42
  • So when I swipe from bottom left to top right, it does the "swipe right" logic. When I swipe from bottom right to top left it also does the "swipe right" logic. Similarly, when I swipe from top to bottom, regardless of angle direction, it does the "swipe left" logic. I have edited my original post to correct something: when I swipe up it does the "swipe right" logic and when I swipe down it does the "swipe left" logic. – Ammer12 Feb 26 '19 at 17:44
  • @ChrisDL I responded but I forgot to tag you haha – Ammer12 Feb 26 '19 at 18:06
  • @Ammer12 the answer Spritzig offered may be a better solution to creating your own swipe detection. However, if you definitely want to get yours working, next thing I'd do is just print out the X & Y values as you do the different swipe cases (If you haven't done that already) – Chris DL Feb 26 '19 at 18:14
  • @ChrisDL my biggest problem is that I am an absolute noob at coding. I am just trying to make due with what limited knowledge I have to finish up this app that I've spent hours on. I have my solution almost working, that's why I'm hesitant to try the better solution offered by Spritzig . That being said, I don't know how to print out the X & Y values haha I apologize for the noob questions, I'm just so close yet feel like it's going way over my head – Ammer12 Feb 26 '19 at 18:19
  • @Ammer12 Definitely understand, honestly I'm only slightly ahead of you. All you would need to do to print out the values is a simply `System.out.println("X: "+ initX+" Y: "+initY);` just before each of your `break;`s – Chris DL Feb 26 '19 at 18:24

0 Answers0