Search code examples
androidmaterial-designnavigation-drawer

Navigation drawer showing current destination name in title


I've developed into my app simple Navigation Drawer mechanism. More about code used in app here: https://developer.android.com/guide/navigation/navigation-ui

The problem's when user's switching between destinations toolbar's title changes so that it displays current location xml layout name:

enter image description here

When restarting or launching app current title is that specified in manifest, when switching to other destination it changes again.

enter image description here

Code of main activity, layout and Kotlin:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    tools:openDrawer="start"
    tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.CurrencyApp.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.CurrencyApp.PopupOverlay">

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:id="@+id/color_mode_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|end"
                android:text="@string/action_color_mode"
                android:textAppearance="@style/TextAppearance.AppCompat.Small" />
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/nav_header"
        android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>

Kotlin file:

class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityMainBinding
    private lateinit var prefs : SharedPreferences
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)
        val navController = findNavController(R.id.fragment)
        val drawerLayout  = binding.drawerLayout
        val appBarConfiguration = AppBarConfiguration(navGraph = navController.graph,drawerLayout)

        binding.toolbar.setupWithNavController(navController,appBarConfiguration)

Nav drawer header:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="176dp"
    android:background="@color/purple_200"
    android:gravity="bottom"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="Testowy nav drawer"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="info@codingflow.com"/>

</LinearLayout>

Nav Drawer menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_message"
            android:icon="@drawable/ic_add_circle_outline_24px"
            android:title="Message" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:title="Share"
                android:icon="@drawable/ic_add" />
        </menu>
    </item>
</menu>

Navigation file:

    <?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.currencyapp.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/main_to_tab"
            app:destination="@id/tabFragment" />
        <action
            android:id="@+id/main_to_card"
            app:destination="@id/recyclerFragment2" />
    </fragment>
    <fragment
        android:id="@+id/recyclerFragment2"
        android:name="com.example.currencyapp.RecyclerFragment"
        android:label="fragment_recycler"
        tools:layout="@layout/fragment_recycler" />
    <fragment
        android:id="@+id/tabFragment"
        android:name="com.example.currencyapp.TabFragment"
        android:label="TabFragment" >
        <action
            android:id="@+id/tab_to_main"
            app:destination="@id/mainFragment" />
    </fragment>
</navigation>

Solution

  • Those titles are not their respective layout xml's names but are android:labels you set in the nav_graph.

    If you want them always be the app's name, set android:label="@string/app_name".