2

After creating a new project with Navigation Drawer Activity, build and run, the activity does not work. It has a number of fragments that it is supposed to change to when they are selected from the activity drawer. Open drawer, select choice, just see: This is home fragment.

2 hours searching web, answer is not clear. Z-Order of DrawerLayout

I Just expect the template to actually work when you run it.

Chuck
  • 339
  • 2
  • 7

2 Answers2

18

So, just to get the Navigation Drawer Activity (template in new... that you can create a new project from) to work at all, you have to edit the activity_main.xml by moving the

<include
        layout="@layout/app_bar_main"

above

<com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"

then, the program will work and you can carry on from there.
so change:

<?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>

To

<?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>

I hope this helps someone not waist as much time as I did. The lesson here is: just because the developers provide a template, doesn't mean it works correctly. So don't automatically assume that it's your fault the program doesn't work.

Chuck
  • 339
  • 2
  • 7
  • 1
    Many thanks ! Works wonderfull here... just terrible to see this is still not fixed... – Zhar Oct 04 '19 at 08:31
1

If you look at the template itself (under the {ANDROID_STUDIO_LOCATION}\plugins\android\lib\templates\activities\NavigationDrawerActivity\root\res-buildApi22\layout\navigation_view.xml.ftl, you can see the correct ordering:

<androidx.drawerlayout.widget.DrawerLayout>

  <include layout="@layout/app_bar_main" />

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

</androidx.drawerlayout.widget.DrawerLayout>

However, there's a known issue with Android Studio 3.5 that causes the reordering of XML attributes. If the include and NavigationView are swapped, you'll get the symptoms you are experiencing. As per the other issue, you need to reset the Code Style of XML to be the pre-defined style 'Android' to fix the underlying issue. Otherwise, your XML files will continue to be reordered.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • So, I need to set the editor xml code style in settings? I found a setting for force rearrange under Editor>Code Style>XML> Arrangement tab. I set that to never. Is that what you mean? – Chuck Sep 08 '19 at 23:37
  • That's certainly one way to brute force it. As per the [other answers on that question](https://stackoverflow.com/a/57668634/1676363), it is really more that you need to add the `attribute` property. Setting the style back to the Android default style just does that for you. – ianhanniballake Sep 09 '19 at 00:15
  • Oh, ok. Sorry, I didn't follow your previous link because, at first glance, it looked like a link to the development issues site which I have never found useful. Now I get it, thanks. It still seems to me that if always rearranging provides a means to, basically, destroy the functionality of somebody's efforts so quickly, that setting it to never rearrange gives us the option to rearrange it on our own, if desired, or leaving it how the developer left it. But I guess rearranging it does give us a standard for when we look at an xml document. Thanks again ianhanniballake – Chuck Sep 10 '19 at 13:51
  • If this helped explain the behavior you're seeing, If appreciate it if you could accept the answer to remove this question from the list of unanswered questions. – ianhanniballake Sep 10 '19 at 14:17