Android in Navigation Drawer When I Click Anywhere Drawer Closes

Android navigation drawer does not close when click outside the drawer (a fragment in main activity)

This happens because you NavigationDrawer is not capturing the view because the activity elements are not included in it, and to have it working properly, you have to make the NavigationDrawer is the main root of your Activity layout and the RelativeLayout is a child of it, like the following:

<android.support.v4.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
tools:context=".MainActivity">

<RelativeLayout 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/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:title="Apartment Guide"
app:titleTextColor="@android:color/white" />

<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar">

</FrameLayout>

</RelativeLayout>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header"
app:itemTextColor="@android:color/darker_gray"
app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

close navigation drawer after clicking on any of its item

Use closeDrawer() method to close the drawer and start your other activity on the listener of drawer.

For Example.

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);

//Start your activity
}

Navigation Drawer after a while , keep closing on touch everywhere of screen

After many test I realized why this problem happened the problem is in my xml I set a view after NavigationView and set that view s visibility to gone so first run every thing is ok but when i set that view s visibility visible whit code for notify user something and after user notified set that view s visibility gone whit code again then NavigationView going crazy and touch event don't response

<android.support.v4.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" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/bg_drower"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>

<View
android:id="@+id/correct_answer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>

</android.support.v4.widget.DrawerLayout>

for fix I changed the code like this and set that view before NavigationView and i dont know why but problem resolved.

<android.support.v4.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" />

<View
android:id="@+id/correct_answer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/bg_drower"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >

</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Navigation Drawer closes on click

I did not realize this but the main container must come before the nav drawer. I moved the ListView for the nav drawer to the bottom of my activity_main.xml and viola, it works!

How to prevent closing Navigation drawer by touch outside the drawer

Based on the other answer which I wrote here. I have modified the code to suit your question. Please check.

Check more about touch hierarchy here

dispatchTouchEvent() method should be overridden in Activity class

@Override    
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (isDrawerOpen()) { //Your code here to check whether drawer is open or not.

View content = findViewById(R.id.drawer); //drawer view id
int[] contentLocation = new int[2];
content.getLocationOnScreen(contentLocation);
Rect rect = new Rect(contentLocation[0],
contentLocation[1],
contentLocation[0] + content.getWidth(),
contentLocation[1] + content.getHeight());

if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
isOutSideClicked = true;
} else {
isOutSideClicked = false;
}

} else {
return super.dispatchTouchEvent(event);
}
} else if (event.getAction() == MotionEvent.ACTION_DOWN && isOutSideClicked) {
isOutSideClicked = false;
return super.dispatchTouchEvent(event);
} else if (event.getAction() == MotionEvent.ACTION_MOVE && isOutSideClicked) {
return super.dispatchTouchEvent(event);
}

if (isOutSideClicked) {
return true; //restrict the touch event here
}else{
return super.dispatchTouchEvent(event);
}
}

Note: As mentioned in the question comments, this is against of Android guidelines. So try to avoid it until unless it is mandatory.

Navigation Drawer closing bug

You can add FragmentLayout with layout_width/layout_height = match_parent property in your main layout. If you don't need this, don't use/call FragmentLayout in your activity. I know, this is not a good solution, but, it works for me.



Related Topics



Leave a reply



Submit