title: Change a button text on android tab swipe or select
body: <p>I have a button on a tabbed activity, now every the activity is swiped or another tab is selected, i want to change the button text based on the fragment's index. How do i do that? </p>
  
  <p>I just followed this tutorial on creating android tabs:
  <a href="https://www.youtube.com/watch?v=00LLd7qr9sA" rel="nofollow noreferrer">https://www.youtube.com/watch?v=00LLd7qr9sA</a>
  It's basically the default tabbed activity, it just created one java and xml file per fragment.</p>
  
  <p>This is my 
  main_activity.java</p>
  
  <pre><code>import android.support.design.widget.TabLayout;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  
  public class MainActivity extends AppCompatActivity{
  
      private SectionsPagerAdapter mSectionsPagerAdapter;
      private ViewPager mViewPager;
      Button buttonChangeMyText;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main_screen);
  
          buttonChangeMyText= (Button)findViewById(R.id.btnChange);
  
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          // Create the adapter that will return a fragment for each of the three
          // primary sections of the activity.
          mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = (ViewPager) findViewById(R.id.container);
          mViewPager.setAdapter(mSectionsPagerAdapter);
  
  
          anoNa=mViewPager.getCurrentItem();
  
  
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(mViewPager);
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main_screen, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  /*
      @Override
      public void onPageSelected(View v) {
          TextView tv = (TextView) v.getRootView().findViewById(R.id.textViewSaUna);
          tv.setText("Welcome To Android");
      }*/
  
  
      /**
       * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
       * one of the sections/tabs/pages.
       */
      public class SectionsPagerAdapter extends FragmentPagerAdapter{
  
          public SectionsPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position) {
              switch (position){
                  case 0:
                      Tab1_First tab1 = new Tab1_First();
                      return tab1;
                  case 1:
                      Tab2_Second tab2 = new Tab2_Second();
                      return tab2;
                  case 2:
                      Tab3_Third tab3 = new Tab3_Third();
                      return tab3;
                  default:
                      return null;
  
              }
  
          }
  
          @Override
          public int getCount() {
              // Show 3 total pages.
              return 3;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              switch (position) {
                  case 0:
                      return "ONE";
                  case 1:
                      return "TWO";
                  case 2:
                      return "THREE";
              }
              return null;
          }
      }//end ng fragment adapter
  }
  </code></pre>
  
  <p>This is the java code of a fragment.</p>
  
  <pre><code>import android.support.v4.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class Tab1_Una extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.tab1_una, container, false);
  
          return rootView;
      }
  
  }
  </code></pre>
  
  <p>this is the xml of the main_activity.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/main_content"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context="com.example.lat.sampletabs.MainScreen"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:id="@+id/appbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:paddingTop="@dimen/appbar_padding_top"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|enterAlways"
              app:popupTheme="@style/AppTheme.PopupOverlay"
  
  
              &gt;
  
          &lt;/android.support.v7.widget.Toolbar&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;Button
          android:id="@+id/btnChange"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="120dp"
          android:layout_marginTop="500dp"
          android:text="Change the text"/&gt;
  
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>