Home » Android » How to enable tap listener in view pager in Android?

How to enable tap listener in view pager in Android?

Posted by: admin June 15, 2020 Leave a comment


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?


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.

How to&Answers:

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;