title: Block page swipe in a view pager programmatically
body: <p>I am developing an app where the user can type in stuff in edittexts and get to the next view by swiping the screen. The swiping-stuff is handled by a viewpager.</p>
  
  <p>What I want to do is the following:</p>
  
  <p>The user should only be able to swipe when all edittexts are filled. Currently all my edittexts have a TextWatcher whichs sets a boolean value to "true" once every field is filled. When it is true, I want to enable the viewpager, when it isn't, I want to disable it.</p>
  
  <p>I'm using a CustomViewPager with three Fragments. I post my CustomViewPager code:</p>
  
  <pre><code>import android.content.Context;
  import android.support.v4.view.ViewPager;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.widget.Toast;
  
  public class CustomViewPager extends ViewPager {
  
  private boolean isPagingEnabled;
  public Context context;
  public CustomViewPager(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.isPagingEnabled = true;
      this.context = context;
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
  
      if (isPagingEnabled) {
          return super.onTouchEvent(event);
      }
      Toast.makeText(context, "Please fill in the details, then swipe !",
              Toast.LENGTH_LONG).show();
      return false;
  }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
      if (isPagingEnabled) {
          return super.onInterceptTouchEvent(event);
  
      }
  
      return false;
  }
  
  public void setPagingEnabled(boolean b) {
      isPagingEnabled = b;
  }
  }
  </code></pre>
  
  <p>PageViewActivity.java</p>
  
  <pre><code>public class PageViewActivity extends FragmentActivity {
      MyPageAdapter pageAdapter;
      int pageId=0;
      public static CustomViewPager pager;
      PageListener pageListener;
      int currentPage=0;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List&lt;Fragment&gt; fragments = getFragments();
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
        pager =(CustomViewPager)findViewById(R.id.viewpager);
  
  
  
        pager.setAdapter(pageAdapter);
        pager.setPagingEnabled(false); 
  
        pageListener = new PageListener();
  
        pager.setOnPageChangeListener(pageListener);
  
      }
  
      private List&lt;Fragment&gt; getFragments(){
       List&lt;Fragment&gt; fList = new ArrayList&lt;Fragment&gt;();
  
       fList.add(MyFragmentA.newInstance("Fragment 1"));
       fList.add(MyFragmentB.newInstance("Fragment 2"));
       fList.add(MyFragmentC.newInstance("Fragment 3"));
  
  
       return fList;
      }
  
      public void next(View v){
  
          if(pageId&lt;=2)
          {
          pager.setCurrentItem(pageId);
          }
          pageId++;
      }
  
      private class PageListener extends SimpleOnPageChangeListener{
  
  
          public void onPageSelected(int position) {
              Log.i("test", "onPageSelected " + position);
              currentPage = position;
  
  
           }
  
          //Called when the scroll state changes.
          public void onPageScrollStateChanged(int state)
          {
  
          }
  
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
          {
          Log.i("test", "onPageScrolled " + position);
              currentPage = position;
  
          }
      }
  
  }
  </code></pre>
  
  <p>My Page Adapter is this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import java.util.List;
  
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.widget.Toast;
  
  class MyPageAdapter extends FragmentPagerAdapter {
   private List&lt;Fragment&gt; fragments;
  
   public MyPageAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) {
     super(fm);
     this.fragments = fragments;
   }
   @Override
   public Fragment getItem(int position) {
     return this.fragments.get(position);
   }
  
   @Override
   public int getCount() {
     return this.fragments.size();
   }
   
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Finally I post one of my Fragments with the only relevant code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class MyFragmentA extends Fragment {
   
   EditText editname;
   public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
   
   
   public static final MyFragmentA newInstance(String message)
   {
     MyFragmentA f = new MyFragmentA();
     Bundle bdl = new Bundle(1);
     bdl.putString(EXTRA_MESSAGE, message);
     f.setArguments(bdl);
     return f;
   }
   
  
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
    
    
    editname = (EditText)myFragmentView.findViewById(R.id.name_input);
    
    editname.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  
               if(editname.getText().toString().trim().length() == 0) {
                
                      // Can I set here isPagingEnabled = false??
                Toast.makeText(getActivity(), "SwipeFlag false!", Toast.LENGTH_SHORT).show();
               
               } else if(editname.getText().toString().trim().length() &gt; 0) {
                
                      // Can I set here isPagingEnabled = true??
                Toast.makeText(getActivity(), "SwipeFlag true!",   Toast.LENGTH_SHORT).show();
               
               }
             }
              
          });
    
    return myFragmentView;
   } 
        
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I know that the keypoint to block page swipe is to set the flag "isPagingEnabled" to false, but I can't understand what is the best way to set it from a Fragment at runtime.</p>