Devrath Devrath - 11 months ago 193
Android Question

How to disable or enable viewpager swiping in android

What i am trying to do: I am trying to Enable/disable swiping in pager programatically when the program is running

Ex: When on the flow if i check for a condition and if it returns

enable swiping, and if condition returns
disable swiping.

Solution i am using is this one

public class CustomViewPager extends ViewPager {

private boolean enabled;

public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;

public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);

return false;

public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);

return false;

public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
} }

Then select this instead of the builtin viewpager in XML

android:layout_width="match_parent" />

You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate.

Problem with above methodology: i cannot set the property on the flow, I:e .... I either can enable swiping or disable swiping. But i cannot do this based on condition


  1. Can i achieve my goal in some-other way, if so what is it ?

  2. Or is this not possible ?

Answer Source

Disable swipe progmatically by-

    final View touchView = findViewById(; 
    touchView.setOnTouchListener(new View.OnTouchListener() 
        public boolean onTouch(View v, MotionEvent event)
           return true; 

and use this to swipe manually

touchView.setCurrentItem(int index);