Search code examples
androidandroid-toolbarandroid-jetpackandroid-architecture-navigation

Jetpack Navigation: Hide label in toolbar


The new Jetpack Navigation uses the label attribute from the navigation.xml file to automatically update the title of the toolbar according to the destination fragment which is shown.

I do not want any title to be shown in one of my destination fragments so I can put a SearchView in this place instead.

I already tried android:label="@null" but the toolbar's title just falls back to the app's title. Calling menu.clear() in the onCreateOptionsMenu() callback also had no effect.

How can I remove the title from my toolbar menu entirely in a specific destination fragment only?


Solution

  • One solution would be to hide the action bar in your specific fragment and create a new ActionBar inside the fragment with your searchview etc. for it. Depending on whether it's a support or normal actionbar it would be something like this in the onCreateView:

    val act: MainActivity = activity as MainActivity
        act.supportActionBar?.hide()
    

    or just activity?.actionBar?.hide()

    Obviously, you'd have to set up your own actionbar in the fragment layout as mentioned above. You could use something like the com.google.android.material.appbar.AppBarLayout with androidx.appcompat.widget.Toolbar inside, or any custom layout, be it from a library or yourself.