1

I ran into this issue while developing a navigation drawer for my app,when the chronology of the child views are as follows everything works like a charm

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main_nav_graph" />

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_menu" />
    </androidx.drawerlayout.widget.DrawerLayout>

and the menu items stop responding to the clicks if the child views are as follows

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_menu" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main_nav_graph" />
    </androidx.drawerlayout.widget.DrawerLayout>

So thereby the chronology of the child views inside the navigation drawer matters, I wanna know why?

Sekiro
  • 1,537
  • 1
  • 7
  • 21

1 Answers1

0

In the documentation for DrawerLayout it says this:

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity>. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

Documentation source: DrawerLayout

So my guess for this is that content needs to be the first child because the menu is sliding over your content. It is probably in the background on how DrawerLayout handles these layouts. So if you put it as a second child it will somehow overlap and disable click events on the menu itself. Hope this explains it.

SlothCoding
  • 1,546
  • 7
  • 14
  • I don't really think the z-index matters because if a view is attached with an onClickListener, irrespective of where and how its placed, the clickListener should be triggered right? and in this case its somehow not getting triggered and I am still not sure why – Sekiro Jan 04 '21 at 14:09
  • @rcs I don't think that onClick should be always triggered. Sometimes I had issues in other layouts with this overlapping and not responding to the onClick event. Since there is nothing in documentation except this it just says that we put it as the first child so it's what happens in the background. There is no other answer I could find I tried to find it because it got me intrigued. If you have time check this v4 DrawerLayout source code, maybe there is a solution: https://android.googlesource.com/platform/frameworks/support/+/d6edbe7/v4/java/android/support/v4/widget/DrawerLayout.java – SlothCoding Jan 04 '21 at 16:33
  • thanks for the link, surely will look into it – Sekiro Jan 04 '21 at 16:47