How to use Bottom Navigation with Navigation Drawer - java

I am trying to add navigation drawer with bottom navigation view. I have full screen activities for all bottom navigation view fragments. And I have app icon top left side. I want to open navigation drawer when user click on that icon. So How can I implement that?
I tried with DrawerLayout. But it includes the toolbar. And I tried to open new Activities on click of item of Navigation drawer but it gives IllegleStateException.

I shared the following code below. You have to put the hamburger menu icon for Nav Drawer in the toolbar and a NavigationView inside the DrawerLayout which has a child (ListView, RecyclerView, ExpandableListView etc.) to be filled programmatically or you can just set static items in the NavigationView by setting the menu in NavigationView. Hope that helps. Do ask if you have any more queries.
<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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorActivityBackground"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:layoutDirection="locale"
android:orientation="vertical"
android:textDirection="locale">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/topbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"/>
</RelativeLayout>
</FrameLayout>
<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"
android:maxWidth="320dp">
<ExpandableListView
android:id="#+id/navList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
android:groupIndicator="#null" />
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

Related

Why is my drawer menu not responding when having a ConstraintLayout on the same XML?

I'm trying to add a drawer menu in two activities, one only has the drawer menu and the other one has the drawer menu and a ConstraintLayout beneath, the one without the ConstraintLayout works good, but the drawer menu of the other one doesn't respond, it doesn't scroll and if I try to select an option it just closes the drawer and does nothing.
Both XML are exactly the same except for the extra ConstraintLayout, if I remove the ConstraintLayout it works well but I need it for some controls.
this is the XML of the activity that doesn't work:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drwlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".ac_Menu">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/linearLayoutCompat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="395dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/mitoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drwamenu"
app:headerLayout="#layout/nav_header"
></com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<ListView
.... />
<TextView
...... />
<TextView
...... />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>

How to show a bottom navigation drawer when clicking navigation icon on BottomAppBar?

So, I made a memo Android app, and I've used the new material BottomAppBar with a FAB, and I want to show a bottom navigation drawer when I press on the hamburger item (R.id.home) on my BottomAppBar, but I all what I've found on the Internet was regular Navigation drawers (that expand from the side).
Also, the BottomAppBar allows me to choose only a still icon for the navigation icon using app:navigationIcon attribute, while I want to have the same animated hamburger icon that converts to an arrow.
Here's my main activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<ListView
android:id="#+id/noteList"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/note_view" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/colorPrimary"
app:fabAlignmentMode="center"
app:fabAnimationMode="slide"
app:fabCradleRoundedCornerRadius="40dp"
app:hideOnScroll="true"
app:navigationIcon="#drawable/ic_menu_24dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_create_24dp"
app:backgroundTint="#color/colorAccent"
app:layout_anchor="#id/bar"
app:rippleColor="#color/rippleColor" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Note: I'm trying to make an app like Reply (a Material design study) that's available on material.io.

Trying to add nav drawer button to the app bar

I'm following this tutorial
I'm stuck at the bit where I'm trying to add the button to the app bar.
When I swipe right, the navigation drawer appears, so thats working. Its just the button in the bar that I'm having problems with, When I add the v7.widget.Toolbar into the XML under FrameLayout it ruins the layout.
Orginally in my XML I just had a constraint layout, with a Scrollview as a child and inside that a LinearView (vertical).
However I cant get my head around why this isnt working:
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<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"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header" />
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/seeds_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="#drawable/customborder"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</ScrollView>
</FrameLayout>
When I run this the top bar just seems to overtake the entire window and the color disappears and the button is not clickable because the LinearLayout is over the top of it. Where am I going wrong?
put NavigationView below FrameLayout.
Following on MikeM's comments above, He said:
FrameLayout will overlap its child Views; i.e., it'll stack them one atop the other in the z-axis. Change the FrameLayout to a vertical LinearLayout, which will lay them out edge to edge. Also, your NavigationView needs to be listed last within the DrawerLayout.
So basically I had change my FrameLayout to Linearlayout and that worked. Its a bit dissapointing that the Android documentation is like that but what I do.
Thanks, MikeM!

Can't drag DrawerLayout from ListView

I have the following MainActivity layout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EAEAEA" />
<LinearLayout
android:id="#+id/navigation_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:clickable="true"
android:orientation="vertical" >
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
The fragment I add on the FrameLayout contains a ListView. The problem is that I cannot drag the DrawerLayout from ListView, only if ListView is long enough to scroll (vertically).
Everything works well for the DrawerLayout, I have tried to open it using openDrawer and it works, I only can't open it by dragging from ListView. Is there any way to fix this?

Android DrawerLayout not sliding actionbar

i have created a slidermenu but the issue i am having is that it does not slide the action bar?
It only slides the content.
Here is my xml of the activity
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.me.ui.MainActivity">
<LinearLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
<ListView
android:id="#+id/side_menu"
android:layout_width="#dimen/slide_menu_width"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="#android:color/black"
android:choiceMode="singleChoice"
android:divider="#null"></ListView>
</android.support.v4.widget.DrawerLayout>
The "native" navigation drawer is intended to slide only the content, not the actionbar.
Some early implementations (i.e. not official) allowed the actionbar to be slided too, but the implementation provided by the support library does not.
Check this answer for example.

Categories