1

I implemented this as the default fragment when the navigation drawer fires up and it works, but the ActionBarDrawerToggle won't appear.

Main Activity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private ActionBarDrawerToggle toggle;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initializing Toolbar and setting it as the actionbar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ServiceFragment serviceFragment = new ServiceFragment();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.content_fragment, serviceFragment)
            .commit();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    //Initializing NavigationView
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    //This method triggers when an item is clicked in navigation drawer menu
    navigationView.setNavigationItemSelectedListener(this);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/content_fragment"/>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

Default fragment, ServiceFragment inflates layout fragment_service.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/service_fragment">

<!-- TODO: Update blank fragment layout -->
    <android.support.design.widget.CoordinatorLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/detail_backdrop_height"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleTextAppearance="@android:color/transparent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <!--android:scaleType="centerCrop"-->
                    <ImageView
                        android:id="@+id/backdrop"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fitsSystemWindows="true"
                        android:scaleType="fitXY"
                        app:layout_collapseMode="parallax" />

                </RelativeLayout>
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

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

    </android.support.design.widget.CoordinatorLayout>

xaldin
  • 23
  • 1
  • 5
  • It's because your `Toolbar` isn't there yet when you create the `ActionBarDrawerToggle`. In fact, `toolbar` will be null throughout `onCreate()`, but `ActionBarDrawerToggle` won't complain about it, so no Exception is thrown. You really should be keeping the `DrawerLayout`, `Toolbar`, and `ActionBarDrawerToggle` all in one place - either the `Activity` or the `Fragment` - but if you really want to keep the current setup, calling `manager.executePendingTransactions()` right after the `commit()` call will probably fix your problem. – Mike M. Sep 13 '16 at 07:05
  • Adding `DrawerLayout`, `Toolbar`, and `ActionBarDrawerToggle` in Fragment does [work](http://imgur.com/a/MEhe1) , but the Action bar won't appear when I scroll down the page - collapsing toolbar - as shown in the image above. No avail by calling `manager.executePendingTransactions()`. Any ideas? – xaldin Sep 13 '16 at 13:30
  • Oh, sorry, I neglected to mention that you'd have to move the `toolbar` initialization to after that `executePendingTransactions()` call. Basically, do the `FragmentTransaction` immediately after `setContentView()`, then do everything else. – Mike M. Sep 13 '16 at 13:43
  • Thanks buddy. That did it – xaldin Sep 13 '16 at 15:54

1 Answers1

0

You need to add toolbar as following:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
      <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/content_fragment"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

& in fragment layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clickable="true"
    android:background="@android:color/white"
    >

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

        </LinearLayout>
Android Geek
  • 8,956
  • 2
  • 21
  • 35