1

Android Studio 4. New Project Navigation Drawer Activity. Add name etc and then build. Send to Nexus 6P API 28 Emulation, Pixel 3a API29 Emulation. Also to Samsung SM-J530Y Android 9 and Nokia 7Plus Android 10.

All instances allow the drawer to be opened and all instances do not allow the drawers to be selected but attempting to select an entry closes the drawer.

I have added an onDestinationChangedListener to the navController and this only kicks out the initial home selection event.

Tried adding navView.setNavigationItemSelectedListener and this is never triggered

Searching only drags up old issues but nothing using the newer NavController. Tried updating to the latest beta01 kotlin dependencies

def navVersion = "2.3.0-beta01"
implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navVersion"

from the template "2.2.2"

Added also the apply plugin: 'androidx.navigation.safeargs.kotlin' to no avail.

Would pull out some hair but I do not have much left.

I am wanting to add drawer navigation away from tabs in my pager app hence the interest in getting this to work.

Gary Robottom
  • 101
  • 1
  • 5
  • 1
    It sounds like your layout might be out of order. The `` that acts as the drawer must be listed last within the ``. Assuming that's still a `` in that particular template, then just make sure it's the last child within the ``. If that was the issue, and you didn't change that layout yourself, let me know. It might've been done by your IDE automatically, and we might have to change a setting to get it to stop doing that. – Mike M. Jun 10 '20 at 00:29
  • I have not altered anything other than adding the handlers in MainActivity onCreate trying to determine whether an event is being generated or not. I have inspected the activity_main.xml and the NavigationView was prior to the include for the content layout. I swapped these around and off she went. It now works. Thankyou Mike M. – Gary Robottom Jun 10 '20 at 02:14
  • No problem. If might've just been a fluke this time, but that template should have that layout in the correct order to begin with. If you notice that this issue comes back – or if you have an issue with any other layout being re-ordered automagically – you might need to adjust a setting to get Android Studio to stop doing that. It's explained in [this post](https://stackoverflow.com/q/57591080). Just FYI. Glad you got it working. Cheers! – Mike M. Jun 10 '20 at 02:22

1 Answers1

1

As highlighted by Mike M in the comments the NavigationView in the activity_main.xml was not in the correct order.

<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
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"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>

is wrong.

<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">



    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

Works. So simple yet so very frustrating.

Gary Robottom
  • 101
  • 1
  • 5