How to change the activity title when the tab changes (slide TabLayout)… here is a solution to the problem.
How to change the activity title when the tab changes (slide TabLayout)
I’ve been searching for a solution for this for about three days. I tried a lot but I can’t use it correctly. (Or maybe I’m doing it wrong).
So, what I want to do is change the activity title to the current tab name, and every time I change the tab, the activity title should also change.
So I have a MainActivity with 3 tabs and 3 different tab fragments. Here is MainActivity:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new CustomPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setViewPager(mPager);
}
@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, 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);
}
Custom ViewPager for the tabs
class CustomPagerAdapter extends FragmentPagerAdapter {
String[] tabsTitles;
String vpPager;
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
tabsTitles = getResources().getStringArray(R.array.tabTitles);
}
@Override
public Fragment getItem(int position) {
if (position == 0){
news_fragment news_fragment = new news_fragment();
return news_fragment;
//}
if (position == 1){
login_fragment login_fragment = new login_fragment();
return login_fragment;
}
else if (position == 2){
about_fragment about_fragment = new about_fragment();
return about_fragment;
}
else {
news_fragment news_fragment = new news_fragment();
return news_fragment;
}
}
@Override
public CharSequence getPageTitle(int position) {
return tabsTitles[position];
}
@Override
public int getCount() {
return 3;
}
}
}
I’m using this SlidingTabLayout
Solution
Try this :
viewpager
.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
TODO Auto-generated method stub
actionBar.setTitle(tabsTitles[position]);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int pos) {
TODO Auto-generated method stub
}
});