title: How do I detect a button pressed in an Android ViewPager?
body: <p>I created a ViewPager that uses individual Fragments. There are 3, here is an example of one of them: </p>
  
  <pre><code>public class PainFragment extends Fragment {
  
      private TextView mTxtScale;
      private Button mBtnMinus;
      private Button mBtnPlus;
      private int mScale;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment_pain, container, false);
  
          mTxtScale = (TextView)v.findViewById(R.id.scale);
          mBtnMinus = (Button)v.findViewById(R.id.minus);
          mBtnPlus = (Button)v.findViewById(R.id.plus);
          mScale = Integer.valueOf(mTxtScale.getText().toString());
  
          mBtnMinus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mScale--;
                  if(mScale == -1) {
                      mScale = 9;
                  }
                  mTxtScale.setText(String.valueOf(mScale));
              }
          });
  
          mBtnPlus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mScale++;
                  if(mScale == 10) {
                      mScale = 0;
                  }
                  mTxtScale.setText(String.valueOf(mScale));
              }
          });
  
  
          return v;
      }
  
      public static PainFragment newInstance(String text) {
  
          PainFragment f = new PainFragment();
          Bundle b = new Bundle();
          //b.putString("msg", text);
  
          f.setArguments(b);
  
          return f;
      }
  
      public int getScale() {
  
          int scale = Integer.valueOf(mTxtScale.getText().toString());
          return scale;
      }
  </code></pre>
  
  <p>And I instantiated the ViewPager in my MainFragment:</p>
  
  <pre><code>public class MainFragment extends Fragment {
  
      Entry mEntry = new Entry();
      ViewPager mPager;
      JournalPagerAdapter mAdapter;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_main_screen, container, false);
  
          mPager = (ViewPager)rootView.findViewById(R.id.pager);
          mPager.setOffscreenPageLimit(2); // So all 3 pages are loaded at once.
          mAdapter = new JournalPagerAdapter(getActivity().getSupportFragmentManager());
  
          mPager.setAdapter(mAdapter);
          ...
  </code></pre>
  
  <p>I have button click listeners in the ViewPager Fragments. I would like to know the best way to set up a listener so that my main fragment can detect when a button is pressed on one of the ViewPager fragments. </p>
  
  <p>/** Update - Here is my adapter class **/</p>
  
  <pre><code>public class JournalPagerAdapter extends FragmentPagerAdapter {
  
      SparseArray&lt;Fragment&gt; registeredFragments = new SparseArray&lt;Fragment&gt;();
  
      public JournalPagerAdapter(FragmentManager mgr) {
          super(mgr);
      }
      @Override
      public Fragment getItem(int pos) {
          switch(pos) {
  
              case 0: return PainFragment.newInstance("PainFragment");
              case 1: return StressFragment.newInstance("StressFragment");
              case 2: return SleepFragment.newInstance("SleepFragment");
              default: return PainFragment.newInstance("PainFragment");
          }
      }
  
      @Override
      public int getCount() {
          return 3;
      }
  
      /* Thanks to Streets of Boston (http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager)
       * for the next 3 methods, should include in all PagerAdapters. Let's you get the fragment instances by position */
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) {
          Fragment fragment = (Fragment) super.instantiateItem(container, position);
          registeredFragments.put(position, fragment);
          return fragment;
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          registeredFragments.remove(position);
          super.destroyItem(container, position, object);
      }
  
      public Fragment getRegisteredFragment(int position) {
          return registeredFragments.get(position);
      }
  
  }
  </code></pre>