Navigation Drawer Closes on Click

How to close navigation drawer when an item is pressed from it?

Got it!

private DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();

Working perfectly fine.

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!

Navigation drawer force close from activity, how to fix it?

<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:layoutDirection="locale">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<ImageButton
android:id="@+id/btn_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_menu"
android:background="@android:color/transparent"
android:layout_margin="15dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

<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:background="@color/white"
app:itemIconTint="@color/white"
app:itemTextColor="@color/text_dark"
app:menu="@menu/activity_main2_drawer">

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

</androidx.drawerlayout.widget.DrawerLayout>

//use this code to get drawer is open or not

drawerLayout = findViewById(R.id.drawer_layout);
findViewById(R.id.btn_nav).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (drawerLayout .isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
drawerLayout.openDrawer(GravityCompat.START)
}
}
});

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
}

Issue in closing navigation drawer when click on Expendable menu item

Issue is here.

new MainActivity().closeDrawer();

Replace with

((MainActivity)mContext).closeDrawer();

You are creating the new instance of the MainActivity every time when you click on the item. Instead of that you can cast the mContext to the MainActivity and call the closeDrawer().

Close navigation drawer after click on item?

There are a few ways you can achieve this. The simplest, in my opinion, would be to simply "watch" the $route object from within your App.vue:

export default {
// ...
watch: {
$route(to, from) {
this.showNav = false
}
}
}

The watch property on the Vue instance contains functions that will watch for changes to variables of the same name. Upon change, the function is run.

More on watchers and computed properties: https://v2.vuejs.org/v2/guide/computed.html

EDIT: Some extra info about reacting to Router changes: https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes



Related Topics



Leave a reply



Submit