With the new Design Library, we\'re supposed to set the toolbar title on the CollapsingToolbarLayout, not the Toolbar itself(at least when using th
In my solution I had to set the title for both the toolbar and the collapsing toolbar for it to work.
So in OnCreate:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
And then further down when I switch fragments I set the title for both when a tab is selected:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//TAB1 - THE DEFAULT TAB
switch (item.getItemId()) {
case R.id.tab_rooms:
toolbar.setTitle("My Title");
collapsingToolbar.setTitle("My Title");
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragment = new RoomsFragment();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
case R.id.tab_shisha:
toolbar.setTitle("My Title2");
collapsingToolbar.setTitle("My Title2");
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragment = new ShishaFragment();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
}
return false;
}
Hope that helps someone!