Search code examples
androidandroid-layoutandroid-fragmentsnavigation-drawerhamburger-menu

Navigation bar hamburger icon not working


I have created the tablayout activity with viewPager for fragment transaction.

Now my design changed and going to add drawer. In that the hamburger menu is not working. When swiping from left it is appearing and when I press the icon it is not showing. I think it may be the layout issue. But I am struggling to fix the layout.

I tried but, the whole fragment is moving top if I change the position of drawer.

Layout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout">
        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/containerView">
        </FrameLayout>
        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/shitstuff"
            app:itemTextColor="@color/black"
            app:menu="@menu/drawermenu"
            android:layout_marginTop="-24dp"/>
    </android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

Solution

  • Separate file for tab layout to attach all the tabs in that.

    app_bar_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
      tools:context="helix.profitkey.hotelapp.SimpleTabsActivity">
    
      <include layout="@layout/status_bar"/>
      <android.support.design.widget.CoordinatorLayout
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"
         tools:context="helix.navigationdrawer.MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
           <android.support.design.widget.TabLayout
               android:id="@+id/tabs"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="@drawable/tab_selected_color"
               app:tabTextColor="@color/silver"
               app:tabSelectedTextColor="@color/white"
               app:tabMode="scrollable"
               app:tabGravity="fill"
               />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
           android:id="@+id/viewpager"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
       </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>
    

    Then the xml is attached in the drawer layout. Inside the drawer I used the navigation view to add the list of options or actions. The navigation view app:menu="@menu/drawermenu" this will add the list of items from the drawermenu.

    mainactivity.xml

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout">
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/violet_light"
            android:layout_gravity="start"
            android:id="@+id/drawerstuff"
            app:itemTextColor="@color/violet_statusbar"
            app:menu="@menu/drawermenu"
            android:theme="@style/NavigationTheme"
            android:layout_marginTop="-24dp"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    @menu/drawermenu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:title="Title"
        android:id="@+id/id"
        android:icon="@drawable/tyre"/>
    
    <item android:title="Title"
        android:id="@+id/id1"
        android:icon="@drawable/tyre"/>
    
    <item android:title="Title"
        android:id="@+id/id2"
        android:icon="@drawable/tyre"/>
    
    
    <item android:title="Title">
        <menu>
            <item
                android:title="Title"
                android:id="@+id/id3"
                android:icon="@drawable/tyre"/>
            <item
                android:title="Title"
                android:id="@+id/id4"
                android:icon="@drawable/tyre"/>
        </menu>
    </item>
    
    <group android:id="@+id/group_settings_id">
    
        <item android:title="Settings"
            android:id="@+id/nav_item_settings"
            android:icon="@drawable/tyre"/>
        <item android:title="About"
            android:id="@+id/nav_item_about"
            android:icon="@drawable/tyre"/>
        <item android:title="Help"
            android:id="@+id/nav_item_write_help"
            android:icon="@drawable/tyre"/>
        <item android:title="LogOut"
            android:id="@+id/nav_item_write_logout"
            android:icon="@drawable/tyre"/>
    </group>
    </menu>