1

I've just begun work on a small little app and would like to change the Toolbar title to the name of the fragment the user has selected from the nav drawer, however upon setting the Toolbar as the action bar to be able to use :

getSupportActionBar.setTitle("[insert category here]") 

The toolbar's hamburger button stops opening the nav drawer when clicked. (You can still open it via dragging in from left).

Does anyone know how to fix this and/or does anyone know of another method to change the toolbar's title? (I found this method via googling).

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Oscar Cooke-Abbott
  • 463
  • 1
  • 6
  • 13

2 Answers2

0

Try this in your Activity:

public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

And this is for your Fragment(you can use it on OnCreate or onResume method):

@Override
public void onResume(){
    super.onResume();

    // Set the title 
    ((MainFragmentActivity) getActivity()).setActionBarTitle("Your title");
}

Also, have a look at:

Change ActionBar title using Fragments

Setting Custom ActionBar Title from Fragment

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

this worked well with me hope it helps anyone facing the same problem (this fix the problem when you press on the hamburger icon to open the drawerLayout)

setSupportActionBar(binding.appBarMain.toolbar)
//to open the navBar by pressing on the hamburger Icon
ActionBarDrawerToggle(this, binding.drawerLayout,binding.appBarMain.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)