George
George

Reputation: 235

Correctly implementing Toolbar and NavigationDrawer

I have an application with one activity that contains the navigation drawer. This activity also houses all the fragments that are associated with the navigation drawer.

What I'd like to do is have each fragment implement its own toolbar since some of these fragments might have a different theme or background for each toolbar in each fragment/screen.

The problem is once I have implemented a toolbar for each fragment, the hamburger icon disappears and is replaced with the back arrow. The navigation drawer still works, but the back arrow is misleading since the arrow should be a burger icon for each of those fragment connected with the navigation drawer.

What would be the correct way to implement a toolbar with the navigation drawer and have the freedom to change any property of the toolbar and keep the hamburger icon as well.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include layout="@layout/standard_toolbar" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/my_toolbar" >
    </FrameLayout>

</RelativeLayout>

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical" >    

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:choiceMode="singleChoice"
        android:divider="@color/navigation_drawer_line_color"
        android:dividerHeight="0.4dp"
        android:listSelector="@drawable/list_selector" />        
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

This is my code in the activity that houses the navigation drawer with child fragments as well as the toolbar.

Upvotes: 0

Views: 143

Answers (1)

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

getActivity().findViewById(R.id.toolbar) will give you reference to your Toolbar in your Fragments. Hence, from this point you can control the background, inflate menus, change title, usually as you would with old ActionBar.

Regarding the theming of the toolbar programmatically, it is not possible.

Upvotes: 1

Related Questions