How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

How to Disable Swiping with Finger in ViewPager

šŸ“š Welcome to our blog post! Today, we are going to address a common issue with ViewPager in Android: disabling paging by swiping with your finger, while still allowing programmatically controlled swipes.

āœ‹ Have you ever encountered a situation where you wanted to give your users the ability to navigate through ViewPager using buttons, but you didn't want them to be able to swipe through the pages with their fingers? Well, you're in luck! We have an easy solution for you.

The Challenge

šŸ” Let's paint the picture: you have a ViewPager containing multiple pages, and below it, you have a set of buttons. When a button is clicked, you want the ViewPager to immediately switch to the corresponding page programmatically using mPager.setCurrentItem(position);. However, you want to disable the ability to swipe between pages using finger gestures.

The Solution

šŸ’” To achieve this functionality, we need to create a custom ViewPager class and override its onTouchEvent method to block touch events. Let's dive into the steps:

1ļøāƒ£ First, extend the ViewPager class and create a custom class, let's call it NonSwipeableViewPager.

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class NonSwipeableViewPager extends ViewPager {

    private boolean isSwipeEnabled = false;

    public NonSwipeableViewPager(Context context) {
        super(context);
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return isSwipeEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return isSwipeEnabled && super.onInterceptTouchEvent(event);
    }

    public void setSwipeEnabled(boolean swipeEnabled) {
        isSwipeEnabled = swipeEnabled;
    }
}

2ļøāƒ£ In your XML layout file, replace the existing ViewPager tag with your custom NonSwipeableViewPager tag.

<com.example.yourpackage.NonSwipeableViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

3ļøāƒ£ Next, in your activity or fragment, find the NonSwipeableViewPager and set setSwipeEnabled(false) to disable swiping with fingers.

NonSwipeableViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setSwipeEnabled(false);

Conclusion

šŸŽ‰ Congratulations! You've successfully disabled swiping with fingers in the ViewPager while retaining the ability to programmatically control the swipes using buttons.

šŸ¤” Ready to implement this solution in your own project? It's as easy as 1, 2, 3. Give it a try, and let us know if you encounter any issues or have any additional questions.

šŸ”— Remember to share this blog post with your friends and colleagues who might find it useful. Happy coding!

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my