21

I have an activity with navigation drawer which replace the main_fragment_container on the activity. When one of the fragments is displayed I want to change the layout of the toolbar and add a spinner to it (and remove it when the fragment is hidden).

My layout looks like that:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<android.support.v7.widget.Toolbar

    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    sothree:theme="@style/AppTheme.ActionBar" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main layout -->
    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav drawer -->
    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.idob.mysoccer.ui.DrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start" />
</android.support.v4.widget.DrawerLayout>

Idob
  • 1,620
  • 4
  • 16
  • 27
  • This would make the drawer open below the toolbar. [The Navigation Drawer is specified in Material Design](http://www.google.com/design/spec/patterns/navigation-drawer.html) to overlap the ToolBar (but not even Google Play does this at the moment). – J.G.Sebring Nov 18 '14 at 13:07
  • I did it on purpose, in my design the drawer doesn't look good when it's over the toolbar. – Idob Nov 18 '14 at 13:15

2 Answers2

43

Not sure what you are trying to accomplish but I think, if possible, you should approach this by letting the fragments customize your toolbar rather than replacing it. Your can let your fragments hide/show views on the toolbar depending on your needs.

Add setHasOptionsMenu(true); in the fragments OnCreateView() and then override onOptionsMenuCreated()

Like this:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.result_list, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.this_frag_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

If you need to do more specific things with the toolbar you can get the instance using

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

J.G.Sebring
  • 5,934
  • 1
  • 31
  • 42
  • I have two different toolbars, standard toolbar with a title and toolbar with spinner instead of the title. I just want to show to toolbar with the spinner when one of my fragments is displayed. – Idob Nov 18 '14 at 10:32
  • 3
    I'd still recommend editing the toolbar rather than replacing. Hide/show your titles and spinners depending on active fragment. – J.G.Sebring Nov 18 '14 at 12:59
  • The implementation of one toolbar works good! Thanks Please edit your answer and I will mark it as answered – Idob Nov 18 '14 at 20:30
  • Nice, glad I could help. My answer clearly recommends editing existing toolbar rather than replacing it. It's the first sentence. Not sure what to edit? – J.G.Sebring Nov 20 '14 at 12:44
  • At the end, the solution was to use one "full" toolbar and hide\show the relevant spinner according to the displayed fragment – Idob Nov 21 '14 at 13:49
  • Ok, I clarified the solution. First sentence now specifically recommends to hide/show views on the toolbar. – J.G.Sebring Dec 05 '14 at 08:14
0

The above answer (accepted answer) by J.G.Sebring works but it keeps both the optionsMenu. The previous one and the new one. To just keep the new menu created for fragment you can add menu.clear()

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    menu.clear()
    inflater.inflate(R.menu.settings_menu, menu)
    super.onCreateOptionsMenu(menu, inflater)
}
Saurabh Mhase
  • 108
  • 1
  • 10