title: Using buttons within a ViewPager
body: <p>I am trying to use a ViewPager where users can interact within each pane. For this ViewPager, I am creating a tutorial where I show users to swipe horizontally to switch pane. When the user reaches the last pane, a button is displayed to the user where they will be navigated to the dashboard. I have read a few articles trying to understand how a button works within a ViewPager, but could not really make much sense.</p>
  
  <p>What the code should do is when a button within a pane within a ViewPager is clicked, the dashboard activity is started. </p>
  
  <p>Here is the full code of the pagerAdapter:</p>
  
  <pre><code>class MyPagerAdapter extends PagerAdapter {
  
   public int getCount() {
  
      return 5;
   }
  
  public Object instantiateItem(View collection, int position) {
  
      View v = new View(collection.getContext());
  
      LayoutInflater inflater = (LayoutInflater) collection.getContext()
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      int resId = 0;
      switch (position) {
      case 0:
          resId = R.layout.tutorial_step1;
      break;
  
      case 1:
          resId = R.layout.tutorial_step2;
  
      break;
  
      case 2:
          resId = R.layout.tutorial_step3;
      break;
  
      case 3:
          resId = R.layout.tutorial_step4;
  
      break;
  
      case 4:
          resId = R.layout.tutorial_complete;
          v = inflater.inflate(R.layout.tutorial_complete, null, false);
          LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
          Button button=(Button)l.findViewById(R.id.bTutorialComplete);
          button.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  //Create an intent that starts a different activity
              }
          });
          break;
      }
      ((ViewPager) collection).addView(v, 0);
  
      return v;
  
  }
  
  @Override
  public void destroyItem(View arg0, int arg1, Object arg2) {
      ((ViewPager) arg0).removeView((View) arg2);
  
  }
  
  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == ((View) arg1);
  
  }
  
  @Override
  public Parcelable saveState() {
      return null;
   }
  }
  </code></pre>
  
  <p>From what I have seen in articles, the code I should be working with is:</p>
  
  <pre><code>public Object instantiateItem(View collection, int position) {
  
      View v = new View(collection.getContext());
  
      LayoutInflater inflater = (LayoutInflater) collection.getContext()
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      int resId = 0;
      switch (position) {
      case 0:
          resId = R.layout.tutorial_step1;
      break;
  
      case 1:
          resId = R.layout.tutorial_step2;
  
      break;
  
      case 2:
          resId = R.layout.tutorial_step3;
      break;
  
      case 3:
          resId = R.layout.tutorial_step4;
  
      break;
  
      case 4:
          resId = R.layout.tutorial_complete;
          v = inflater.inflate(R.layout.tutorial_complete, null, false);
          LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
          Button button=(Button)l.findViewById(R.id.bTutorialComplete);
          button.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  //Create an intent that starts a different activity
              }
          });
          break;
      }
      ((ViewPager) collection).addView(v, 0);
  
      return v;
  
  }
  </code></pre>
  
  <p>It is only <code>case 4:</code> within the <code>switch</code> statement that I am working with as this has a button whereas the other layouts only contain images and text which requires no interaction. </p>
  
  <p>I have been working on this for countless hours now and still couldn't figure out a way to resolve this. I would be very grateful if you can help me with this.</p>