Showing posts with label TouchEvent. Show all posts
Showing posts with label TouchEvent. Show all posts

Wednesday, June 26, 2013

Android GestureDetector Example

In android can listen to the gestures and events performed by user on screen.

To listen the Gestures in Android we need to do 3 things
1: Create Class GestureListener which should extends GestureDetector.SimpleOnGestureListener
2: Override s all the callback methods of GestureDetector.SimpleOnGestureListener
3:  Bind the gestureDetector to GestureListener

 GestureListener.java


class GestureListener extends GestureDetector.SimpleOnGestureListener
{
   
       static String currentGestureDetected;
      
      // Override s all the callback methods of GestureDetector.SimpleOnGestureListener
      @Override
      public boolean onSingleTapUp(MotionEvent ev) {
          currentGestureDetected=ev.toString();
      
        return true;
      }
      @Override
      public void onShowPress(MotionEvent ev) {
          currentGestureDetected=ev.toString();
       
      }
      @Override
      public void onLongPress(MotionEvent ev) {
          currentGestureDetected=ev.toString();
      
      }
      @Override
      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
          currentGestureDetected=e1.toString()+ "  "+e2.toString();
     
        return true;
      }
      @Override
      public boolean onDown(MotionEvent ev) {
          currentGestureDetected=ev.toString();
       
        return true;
      }
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
          currentGestureDetected=e1.toString()+ "  "+e2.toString();
        return true;
      }
}


MainActivity.java

public class MainActivity extends Activity
{
          
            private GestureDetector mGestureDetector;
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                   
                    // Bind the gestureDetector to GestureListener
                    mGestureDetector = new GestureDetector(this, new GestureListener());
            }

            // onTouch() method gets called each time you perform any touch event with screen
            @Override
            public boolean onTouchEvent(MotionEvent event)
            {
                //method onTouchEvent of GestureDetector class Analyzes the given motion event
                //and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied.
                //Returns true if the GestureDetector.OnGestureListener consumed the event, else false
.
               
                boolean eventConsumed=mGestureDetector.onTouchEvent(event);
                    if (eventConsumed)
                    {
                        Toast.makeText(this,GestureListener.currentGestureDetected,Toast.LENGTH_LONG).show();
                        return true;
                    }
                    else
                        return false;
            }
}



Friday, June 21, 2013

Detecting Swipe Event In Android

To detect Touch Event and other events like Left to Right sweep and Right to Left sweep, we use  MotionEvent  class.

MotionEvent


MotionEvent object is used to report movement (mouse, pen, finger, trackball) events. Motion events may hold either absolute or relative movements and other data, depending on the type of device.

Motion events describe movements in terms of an action code and a set of axis values. The action code specifies the state change that occurred such as a pointer going down or up. The axis values describe the position and other movement properties.

For example, when the user first touches the screen, the system delivers a touch event to the appropriate View with the action code ACTION_DOWN and a set of axis values that include the X and Y coordinates of the touch and information about the pressure, size and orientation of the contact area.


In the code below I have described with proper Source Code about detecting Swap Events but first we should understand the basics.

onTouchEvent () method ogets called when User performs any touch event on screen

when a User swaps from Left to Right or Right to left

user first touches on the screen ( lets say first x coordinate is x1) holds ,swaps  then leaves the screen  (lets say second x coordinate is x2)

so if x2> x1  it means Left to Right sweep has been performed and
     if x2<x1   it means Right to Left sweep has been performed

Similarly we can track UP to Down and Down to UP swap

if  y2> y1  it means UP to Down sweep has been performed and
if  y2<y1   it means Down to UP sweep has been performed

Example with Source Code


main.xml


TouchEvent



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

        <TextView
            android:layout_marginTop="80dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"
            android:textColor="#000099"
            android:textSize="30dp"
            android:text="Detect Swap Event Demo" />
       
   
</LinearLayout>




MainActivity.java


public class MainActivity extends Activity
{
            float x1,x2;
            float y1, y2;
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);
            }
          
            //
onTouchEvent () method gets called when User performs any touch event on screen 
           // Method to handle touch event like left to right swap and right to left swap
                        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;
                        }

  
}





TouchEvent