Handling both click and touch in android

62 views Asked by At

I have a linear layout, inside which is another linear layout with a grid of buttons(5*5). I want to detect both the click on the buttons, and also swipe action on my complete screen. How To Achieve it?

1

There are 1 answers

1
Drim On

Try this way

float x1,x2;
float y1, y2;


public boolean onTouchEvent(MotionEvent touchevent) 


                    {
                                 switch (touchevent.getAction())
                                 {
                                        // when user first touches the screen we get x and y coordinate
                                         case MotionEvent.ACTION_DOWN: 
                                         {
                                             x1 = touchevent.getX();
                                             y1 = touchevent.getY();
                                             break;
                                        }
                                         case MotionEvent.ACTION_UP: 
                                         {
                                             x2 = touchevent.getX();
                                             y2 = touchevent.getY(); 

                                             / /if left to right sweep event on screen
                                             if (x1 < x2) 
                                             {
                                                 Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
                                              }

                                             // if right to left sweep event on screen
                                             if (x1 > x2)
                                             {
                                                 Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
                                             }

                                             // if UP to Down sweep event on screen
                                             if (y1 < y2) 
                                             {
                                                 Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
                                             }

                                             / /if Down to UP sweep event on screen
                                             if (y1 > y2)
                                             {
                                                 Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
                                              }
                                             break;
                                         }
                                 }
                                 return false;
                    }

}