Sunday 24 June 2012

ANDROID - Swipe Gesture


For most of us newbees, we all know how to simply decorate the xmls but man when it comes to slight animation, we are screwed quite literally ! The latest problem we faced was related to the swipe gesture !
After understanding the whole deal, we thought man, we must make a library where the gestures could be detected and user could simplify its use on the lines of usage of events like - onClickListner  / onTouchListner. So we started to code ! But man we love GOOGLE :)

We got link to this post. The guy has done all that we planned to do. Simply awesome!
So without taking those pains, we used his library and it worked like a charm! It gives you the direction of the swipe, the displacement, and whether it was really a swipe or a click ! I think all that one amateur would want !

The link to his blog is - Awesome Swipe Gesture Implementation

However, we would like to present the jist of the same in our words here in this blog as well.

Steps To Create -

  1. Copy this project into your workspace - Swipe Me
  2. Once done, add the .jar file to your library.
  3. Import the “SwipeMe.OnSwipeListener”.
  4. Set OnTouchlistener to the view object by sending OnSwipeListener as the listener.
  5. Override the onSwipe() method and do whatever you want
  6. Use the getSwipedTo() to know the direction of the swipe

The sample code -

Here is a sample code adding the swipe action and implementing the four direction swipe actions on a button:
 
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
// importing the onSwipeListener
import SwipeMe.OnSwipeListener;

public class SwipeMeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button test = (Button) findViewById(R.id.button1);

        //adding onTouch action to test button
        //but by sending it the on swipe listener
        test.setOnTouchListener(new OnSwipeListener() {

            @Override
            public void onSwipe(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                //checking the direction of the swipe
                switch(getSwipedTo())
                {
                case SWIPED_LEFT :
                    //swiped to left
                    Toast.makeText(getApplicationContext(), "Swiped Left",
                    Toast.LENGTH_SHORT).show();
                    break;
                case SWIPED_RIGHT :
                    //swiped to right
                    Toast.makeText(getApplicationContext(), "Swiped Right",
                    Toast.LENGTH_SHORT).show();
                    break;
                case SWIPED_UP :
                    //swiped up
                    Toast.makeText(getApplicationContext(), "Swiped Up",
                    Toast.LENGTH_SHORT).show();
                    break;
                case SWIPED_DOWN :
                    //swiped down
                    Toast.makeText(getApplicationContext(), "Swiped Down",
                    Toast.LENGTH_SHORT).show();
                    break;
                case CLICKED:
                    //clicked
                    Toast.makeText(getApplicationContext(), "clicked",
                    Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        });
    }
}





2 comments:

  1. thanks..this is helpful

    ReplyDelete
  2. That link is not working..
    please.. can you share the jar file....

    ReplyDelete

Feel free to contact us via our website - www.blotcanvas.com