title: How to disable or enable viewpager swiping in android
body: <p><strong>What i am trying to do:</strong> I am trying to Enable/disable swiping in pager programatically when the program is running </p>
  
  <p><strong><em>Ex:</em></strong> When on the flow if i check for a condition and if it returns <code>true</code> enable swiping, and if condition returns <code>false</code> disable swiping.</p>
  
  <hr>
  
  <h2><a href="https://stackoverflow.com/a/13437997/1083093">Solution i am using is this one</a></h2>
  
  <pre><code>public class CustomViewPager extends ViewPager {
  
  private boolean enabled;
  
  public CustomViewPager(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.enabled = true;
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      if (this.enabled) {
          return super.onTouchEvent(event);
      }
  
      return false;
  }
  
  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
      if (this.enabled) {
          return super.onInterceptTouchEvent(event);
      }
  
      return false;
  }
  
  public void setPagingEnabled(boolean enabled) {
      this.enabled = enabled;
  } }
  </code></pre>
  
  <p>Then select this instead of the builtin viewpager in XML</p>
  
  <pre><code>&lt;mypackage.CustomViewPager 
  android:id="@+id/myViewPager" 
  android:layout_height="match_parent" 
  android:layout_width="match_parent" /&gt;
  </code></pre>
  
  <p>You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate.</p>
  
  <hr>
  
  <p><strong>Problem with above methodology:</strong> 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</p>
  
  <hr>
  
  <h2>Question:</h2>
  
  <ol>
  <li>Can i achieve my goal in some-other way, if so what is it ?</li>
  <li>Or is this not possible ?</li>
  </ol>