How to enable tap listener in view pager in Android?

I am using view pager to swipe between the views in Android.
Now I need to capture tap event for each of the views. when I override the touch listener to capture the tap event, the swipe action doesn’t happen and the screen remains in the first page itself. How do I add touch listener to view pager?


  • Android: Get the listview item from button clicked in custom listview
  • Android: Could not find
  • Should I use ActionBarSherlock for Android 4.0+ apps?
  • Android, NSD/DNS-SD: NsdManager unreliable discovery and IP resolution
  • NoClassDefFoundError on apps that doesn't have newrelic installed
  • want to make some views invisible during runtime in android application
  • viewPager.setOnTouchListener(new OnTouchListener(){
                public boolean onTouch(View v, MotionEvent event)
                         return true;   

    For the above code I am able to capture tap event, but the swipe action becomes Impossible.

    Related posts:

    What kind of constructor is this, beginning with <T extends Drawable & DrawerToggle&a...
    How to know whether user has changed the state of toggle button?
    Adding Overlay to OSMDROID
    Android on-screen keyboard auto popping up with `ActionBarActivity` no menu shows
    Installing ADT plugin in Eclipse for Android development
  • C/C++ printfs - Where's it appears in a Android native code?
  • Starting and stopping a notification from broadcast receiver
  • Retrofit + Okhttp cancel operation not working
  • Make Bluetooth on Android 2.1 discoverable indefinitely
  • How to use a contract class in android?
  • how to make stroke for 3 sides for a shape in android?
  • 5 Solutions collect form web for “How to enable tap listener in view pager in Android?”

    Here i leave you a snippet from my code to detect a “click” on the OnTouchListener, i hope it helps

    mImagePager.setOnTouchListener(new OnTouchListener() {
            private float pointX;
            private float pointY;
            private int tolerance = 50;
            public boolean onTouch(View v, MotionEvent event) {
                case MotionEvent.ACTION_MOVE:
                    return false; //This is important, if you return TRUE the action of swipe will not take place.
                case MotionEvent.ACTION_DOWN:
                    pointX = event.getX();
                    pointY = event.getY();
                case MotionEvent.ACTION_UP:
                    boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
                    boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
                    if(sameX && sameY){
                        //The user "clicked" certain point in the screen or just returned to the same position an raised the finger
                return false;

    We can use Gestures (Link1, Link2):

    public boolean onTouchEvent (MotionEvent ev)

    Hope this helps!

    Nancy, you don’t need to manually override the Page swipes or the touch events. Just add the pages to the ViewPager and the ViewPager will automatically take care of swiping.

    You do, however, have to attach touch listeners to the object in each page. So if Page 1 has a Linear Layout with many buttons and you need to find out when those buttons are clicked, you need to attach OnClickListeners for each of those buttons.

    Do let me know your use case so we can better understand, why you need to find out when a page has been clicked!

    Just to add to Jorge’s great answer, you may just use distance instead of sameX and sameY, which is a bit more elegant. Sample:

    // Ignore events that are swipes rather then touches
    float distX = event.getX() - pointX;
    float distY = event.getY() - pointX;
    double dist = Math.sqrt(distX * distX + distY * distY);
    if (dist > tolerance) {
      return false;

    Put the click event on the item view of the viewpager inside the viewPagerAdapter in the method instantiateItem like –

            public Object instantiateItem(ViewGroup container, final int position) {
                // Declare Variables
                ImageView jive_image;
                inflater = (LayoutInflater) context
                View itemView = inflater.inflate(R.layout.list_item_viewpager, container,
                itemView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                // Add viewpager_item.xml to ViewPager
                ((ViewPager) container).addView(itemView);
                return itemView;
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.