0

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!

phpdroid
  • 1,642
  • 1
  • 18
  • 36

3 Answers3

3

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.

0

You have to setSupportActionBar first.

Your implementation should be like this:

  toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(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();
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
0

Add below code ;

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57