0

I am experiencing a weird issue. I created a navigation view inside a drawer layout, however navigation view is just visible once the activity starts. The toolbar hamburger menu is not clickable.

main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view"
    >
    <group android:checkableBehavior="single">
    <item android:id="@+id/menu_explore" android:title="@string/explore"
        android:icon="@drawable/exploreicon" />

    <item android:id="@+id/menu_profile" android:title="@string/profile"
        android:icon="@drawable/profileicon"/>

    <item android:id="@+id/menu_search" android:title="@string/search"
        android:icon="@drawable/searchicon"/>

    <item android:id="@+id/menu_settings" android:title="@string/settings"
        android:icon="@drawable/settingsicon"/>

    <item android:id="@+id/menu_bookmarked" android:title="@string/bookmarked"
        android:icon="@drawable/bookmarkicon"/>

    <item android:id="@+id/menu_logout" android:title="@string/log_out"
        android:icon="@drawable/logouticon"/>

    </group>
</menu>

activity_main.xml

<androidx.drawerlayout.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/drawerLayout"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_gravity="start"
    android:clickable="false"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:padding="16dp"
    android:scrollIndicators="start"
    android:visibility="visible"
    tools:openDrawer="start">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/mainToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </com.google.android.material.appbar.AppBarLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#FFF"
        app:headerLayout="@layout/menu_header"
        app:itemIconTint="#6b6b6b"
        app:itemTextColor="#6b6b6b"
        app:menu="@menu/main_menu">

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

style for main activity:

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

Main Activity:

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.mainToolbar);
        toolbar.setTitle(R.string.app_name);
        setSupportActionBar(toolbar);
        drawerLayout = findViewById(R.id.drawerLayout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,
                R.string.open,R.string.close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }
}

I want the navigation view to be only visible when the hamburger icon in the toolbar is clicked. Every tutorial I followed did the same things with me, however mine doesn't show inside the toolbar.

Sinan Yaman
  • 5,714
  • 2
  • 15
  • 35
  • Is swipe open the drawer? – Md. Asaduzzaman Dec 11 '19 at 21:32
  • 1
    You're missing the `layout_gravity` attribute on the ``; e.g., `android:layout_gravity="start|left"`. It doesn't go on the ``, if that's why you have that there. – Mike M. Dec 11 '19 at 21:38
  • 1
    yep, layout_gravity was the issue. Thanks Mike. – Sinan Yaman Dec 11 '19 at 21:40
  • 1
    No problem. I misread your `ActionBarDrawerToggle` constructor call, though, so you will have to either pass the `Toolbar` there, or override the `Activity`'s `onOptionsItemSelected()` method, as shown in the last linked duplicate. Sorry 'bout that. Cheers! – Mike M. Dec 11 '19 at 21:44

1 Answers1

1

Pass toolbar instance during initiation of ActionBarDrawerToggle

actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar, R.string.open,R.string.close);
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46