title: Android : I have a toolbar with a sliding tab . Now how do I change data on my toolbar , when user changes the tab
body: <p>I have a toolbar with a sliding tab . Now how do I change data on my toolbar , when user changes the tab . The <a href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java" rel="nofollow">SlidingTabLayout</a> and <a href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java" rel="nofollow">SlidingTabStrip</a> are used . The onPageSelected is overidden in SlidingTabLayout.</p>
  
  <p>toolbar_search.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/ColorPrimary"
  android:elevation="2dp"
  android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" &gt;
  
  &lt;AutoCompleteTextView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/autocomplete"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:drawableLeft="@drawable/ic_action_search"
      android:hint=" Bus Service Number"
      android:textColor="#000" /&gt;
  &lt;/android.support.v7.widget.Toolbar&gt;
  </code></pre>
  
  <p>SearchByBusInfo.java(Main Activity)</p>
  
  <pre><code>public class SearchByBusInfo extends AppCompatActivity  {
  
  // Declaring Your View and Variables
  
  Toolbar toolbar;
  ViewPager pager;
  ViewPagerAdapter adapter;
  SlidingTabLayout tabs;
  CharSequence Titles[] = { "tab1", "tab2" };
  int Numboftabs = 2;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_search_by_bus_info);
  
      // Creating The Toolbar and setting it as the Toolbar for the activity
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles
      // fot the Tabs and Number Of Tabs.
      adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles,
              Numboftabs);
  
      // Assigning ViewPager View and setting the adapter
      pager = (ViewPager) findViewById(R.id.pager);
      pager.setAdapter(adapter);
  
      // Assiging the Sliding Tab Layout View
      tabs = (SlidingTabLayout) findViewById(R.id.tabs);
      tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true,
                                      // This makes the tabs Space Evenly in
                                      // Available width
  
      // Setting Custom Color for the Scroll bar indicator of the Tab View
      tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
          @Override
          public int getIndicatorColor(int position) {
              return getResources().getColor(R.color.tabsScrollColor);
          }
      });
  
      // Setting the ViewPager For the SlidingTabsLayout
      tabs.setViewPager(pager);
  
  }
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main_menu, 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);
  }
  
  
  }
  </code></pre>
  
  <p>ViewPageAdapter.java</p>
  
  <pre><code>public class ViewPagerAdapter extends FragmentStatePagerAdapter {
  
  CharSequence Titles[]; // This will Store the Titles of the Tabs which are
                          // Going to be passed when ViewPagerAdapter is
                          // created
  int NumbOfTabs; // Store the number of tabs, this will also be passed when
                  // the ViewPagerAdapter is created
  
  // Build a Constructor and assign the passed Values to appropriate values in
  // the class
  public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[],
          int mNumbOfTabsumb) {
      super(fm);
  
      this.Titles = mTitles;
      this.NumbOfTabs = mNumbOfTabsumb;
  
  }
  
  // This method return the fragment for the every position in the View Pager
  @Override
  public Fragment getItem(int position) {
  
      if (position == 0) // if the position is 0 we are returning the First
                          // tab
      {
  
          Tab1 tab1 = new Tab1();
  
          return tab1;
      } else // As we are having 2 tabs if the position is now 0 it must be 1
              // so we are returning second tab
      {
  
          Tab2 tab2 = new Tab2();
          return tab2;
      }
  
  }
  
  // This method return the titles for the Tabs in the Tab Strip
  
  @Override
  public CharSequence getPageTitle(int position) {
      return Titles[position];
  }
  
  // This method return the Number of tabs for the tabs Strip
  
  @Override
  public int getCount() {
      return NumbOfTabs;
  }
  </code></pre>
  
  <p>}</p>