Search code examples
androidnavigationandroid-toolbarnavigation-drawer

Navigation Drawer toggle button not working


After searching similar questions like this but not able to get results
I have already defined a toggle button in my activity like

toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);

and here is my xml where i have defined toolbar

<android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <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" >

                <SearchView
                    android:id="@+id/searchView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:iconifiedByDefault="false"
                    android:queryHint="Search"
                    android:layout_centerHorizontal="true"
                    android:focusable="false"/>

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable"
                app:tabGravity="fill"/>
        </android.support.design.widget.AppBarLayout>

here is what I am able to achieve after this
enter image description here enter image description here
As you can see I am not able to get Navigation toggle button on my toolbar
any help will be appreciated thanks!


Solution

  • try to put setSupportActionBar(toolbar); before you define drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    EDIT:

    after i check your app_main_layout.xml, there's also a

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <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.AppBarLayout>
    

    in there, i think this make it confuse because its also have a same id which is @+id/toolbar in your activity.xml

    you have to change the id, or remove it, and its solved.