Search code examples
androidandroid-actionbarnavigation-drawertoolbarhamburger-menu

Using setSupportActionBar() stops hamburger button when opening the navigation drawer


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).


Solution

  • 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