0

After creating a new project and select Navigation Drawer Activity using Kotlin language in Android Studio, the navigation is working like a charm. But when I generate the same project with Java language, it won't navigate to another fragment. What's wrong with this? Anyone can help me?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Husni Kamal
  • 75
  • 1
  • 8
  • Please provide more information such as Exception stacktrace, code which cause problem or the things you tried to fix it – Sanket Bhat Nov 07 '19 at 04:15
  • I just create a new project with Navigation Drawer Activity and Java language, but when I click menu, it's not working. I'm not add some code or anything. It's just a fresh project @SanketBhat – Husni Kamal Nov 07 '19 at 04:20
  • 1
    @HusniKamal : If you don't provide more information it's impossible for anybody to tell what's wrong on your end and it's likely that the question gets closed. If you don't know what the relevant code is, maybe uploading the project to github would allow people to see the problem. – Christian Nov 07 '19 at 08:39
  • "After creating a new project" is not clear? It's ok and thanks for the response :) @Christian – Husni Kamal Nov 09 '19 at 16:12

1 Answers1

1

I have got the same issue.

My solution is:

Modify main_activity.xml: move NavigationView tag to the end of DrawerLayout

How to get this issue

environment:

  • Android Studio: 3.5
  • compileSdkVersion: 29
  • buildToolsVersion: "29.0.0"
  • targetSdkVersion: 29

steps:

  • Step in Android Studio
    • File -> new -> new Module -> Phone & Tablet Module -> Next -> input "test" as Application/Library name -> Next -> Navigation Drawer Activity -> Next - > Finish
    • Select module "test" and Click Run button
  • Step in android phone
    • the page is showing HomeFragment by default
    • click menu button to slide in the DrawerLayout menu
    • click gallery in the menu gallery
    • the DrawerLayout closed but fragment has not change to GalleryFragment

Solution

Finally, i found the solution:

Modify main_activity.xml: move NavigationView tag to the end of DrawerLayout

android studio generated the default main_activity.xml is:

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

we should change it like this(move NavigationView to the end of DrawerLayout):

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

How this issue comes

We can find the reason with source code of DrawerLayout and ViewDragHelper:

DrawerLayout

public class DrawerLayout extends ViewGroup {
    //..........
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getActionMasked();
        //.........
        boolean interceptForTap = false;

        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                final float x = ev.getX();
                final float y = ev.getY();
                if (mScrimOpacity > 0) {
                    /////////////////
                    //
                    //  take a look at: ViewDragHelper.findTopChildUnder(x, y) 
                    //  it finds child from last to first
                    //
                    /////////////////
                    final View child = mLeftDragger.findTopChildUnder((int) x, (int) y);
                    if (child != null && isContentView(child)) {
                        // if child is the contentView of DrawerLayout, intercept it
                        interceptForTap = true;
                    }
                }
                //.........
                break;
            }
            //...........
        }

        return interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch;
    }
    //...........
}

ViewDragHelper

public class ViewDragHelper {
    //.........
    @Nullable
    public View findTopChildUnder(int x, int y) {
        final int childCount = mParentView.getChildCount();
        for (int i = childCount - 1; i >= 0; i--) {
            final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i));
            if (x >= child.getLeft() && x < child.getRight()
                    && y >= child.getTop() && y < child.getBottom()) {
                return child;
            }
        }
        return null;
    }
    //........
}
luckybilly
  • 221
  • 1
  • 9
  • Update: When I create a new project with Navigation Drawer Activity, the activity_main has changed, same as your code @billy – Husni Kamal Nov 09 '19 at 16:18