MML
MML

Reputation: 44

How to change the app toolbar name with navigation drawer item name

I see in a certain app that the text of top toolbar "app name" of an android app is changing when I click on any item of navigation drawer items. To relate see the pics of current task and the destination.

toolbar name sidebar

destination: I want to change it like this instead of the name of app

dest 01 dest 02

This is activity_main_drawer.xml

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/articles"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/articles" />
        <item
            android:id="@+id/windows"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/windows" />
        <item
            android:id="@+id/linux"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/linux" />
        <item
            android:id="@+id/android"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/android" />

        <item
            android:id="@+id/miscellaneous_devices"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/miscellaneous_devices" />

        <item
            android:id="@+id/information_security"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/information_security" />

        <item
            android:id="@+id/facebook"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/facebook" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

The onNavigationItemSelected method:

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    switch (item.getItemId()) {
        case R.id.articles:

            break;
        case R.id.windows:

            break;
        case R.id.linux:

            break;
        case R.id.android:

            break;
        case R.id.miscellaneous_devices:

            break;
        case R.id.information_security:

            break;
        case R.id.facebook:

            break;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Upvotes: 0

Views: 2003

Answers (1)

Dmytro Ivanov
Dmytro Ivanov

Reputation: 1310

Toolbar is just an another UI component.

Case 1: Default toolbar which coming with theme.

getSupportActionBar().setTitle("Your drawer title");

Case 2: Custom toolbar

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("Your drawer title");

Upvotes: 3

Related Questions