How to add navigation drawer to an exisiting activity/layout - android

I'm trying to add navigation drawer to this layout. It currently has a Bottom Navigation View and a relative layout to hold fragment.
Problem is I can't make the root layout a Drawer Layout coz it opts out the fragment container relative layout.
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="deb.com.firebasedemo.HomePage"
>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:background="#054ec5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="#drawable/nav_state_color"
app:itemTextColor="#drawable/nav_state_color"
app:menu="#menu/bottom_nav_menu"
android:layout_alignParentBottom="true"
>
</android.support.design.widget.BottomNavigationView>
<RelativeLayout
android:id="#+id/home_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation">
</RelativeLayout>
</RelativeLayout>
I tried something like this. Putting drawer layout inside the relative layout and navigation view.
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="deb.com.firebasedemo.HomePage"
>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:background="#054ec5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="#drawable/nav_state_color"
app:itemTextColor="#drawable/nav_state_color"
app:menu="#menu/bottom_nav_menu"
android:layout_alignParentBottom="true"
>
</android.support.design.widget.BottomNavigationView>
<RelativeLayout
android:id="#+id/home_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation">
</RelativeLayout>
</RelativeLayout>
But it's not working :( .. The navigation drawer won't show up.
I need some help here.

Related

Grey rectangle shown on drawer NavigationView when hiding android home buttons

My screen doesn't show the android home buttons and I'm running Android 7.0. I see a grey rectangle in the bottom, the height of where the buttons would be:
activity_payment.xml:
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".ui.activities.PaymentActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/status_bar_height"
android:orientation="vertical">
</androidx.constraintlayout.widget.ConstraintLayout>
<include
android:id="#+id/navigation_layout"
layout="#layout/navigation_layout" />
</androidx.drawerlayout.widget.DrawerLayout>
I tried android:elevation="10dp" and android:translationZ="10dp":
navigation_layout.xml:
<com.google.android.material.navigation.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:elevation="10dp"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#drawable/drawer_selector"
app:itemTextColor="#drawable/drawer_selector"
app:menu="#menu/navigation_drawer">
</com.google.android.material.navigation.NavigationView>
nav_header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_header"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:orientation="vertical">
<ImageView
android:id="#+id/drawer_company_logo"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:gravity="start"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:scaleType="fitStart"
android:layout_gravity="start"
android:paddingBottom="#dimen/padding_small"
app:srcCompat="?brandingLogo" />
</LinearLayout>
DrawerLayout
CoordinatorLayout
AppBarLayout
...
NavigationView
Try using it like this.
or try this
Grey bar appears on navigation drawer

Two DrawerLayout in same direction within one activity

Here is my activity xml file with one drawerlayout. how can i add second drawer with same gravity, i mean two drawer layout with same gravity android:layout_gravity="end",
i have requirement to show one more view with slide animation.
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_filter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

Android RelativeLayout Z Order Layering

I have an issue with my activity_main.xml in which the bottombar as shown in my layout below appears to cut off the bottom of my main fragment container and is layered on top of it instead of appearing below it.
The bottombar should appear below the main conatiner instead of layering above it.
Can someone please help me as I have tried everything.
<?xml version="1.0" encoding="utf-8"?>
<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_new"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/my_toolbar"
layout="#layout/toolbar">
</include>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragmentContainerNew"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomBar" />
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBarNew"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="#xml/bottombar_tabs" />
</RelativeLayout>
</LinearLayout>
<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/activity_main_drawer" />
bottom part of activity_main
You forgot to add proper id #+id/bottomBarNew, Change your value:
<FrameLayout
android:id="#+id/fragmentContainerNew"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomBarNew" /> //<---here

Why is my BottomBar in the middle of the screen?

I'm using both NavigationDrawer and BottomNavigationView. But after the add Navigation Drawer, position of Bottom Navigation is changed. How do I solve that? I assume this problem is caused by my xml files.
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context="com.example.yunus.ototakip.MainActivity">
<include
layout="#layout/app_bar_navigation_bar"
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"
app:headerLayout="#layout/nav_header_navigation_bar"
app:menu="#menu/activity_navigation_bar_drawer" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#color/beyaz"
app:itemTextColor="#color/beyaz"
app:menu="#menu/bottombar_menu" />
Attach your BottomNavigationView inside the CoordinatorLayout (or whatever view group you've inside app_bar_navigation_bar layout) not DrawerLayout and add appropriate gravity tag.
android:layout_gravity="bottom"
Try using either of the two=
android:layout_gravity="bottom"
or
android:gravity="bottom"
use android:layout_gravity="bottom" instead of layout_alignParentBottom as it is for RelativeLayout not android.support.v4.widget.DrawerLayout.

Relative layout always seems to work for only drawer layout or frame layout, both child doesn't work together

I have a relative layout in which i have two child .one is drawer layout and the other is a frame layout in which i have used a fragment .The problem is when i keep drawer layout in the top and the frame layout in the bottom,drawer layout doesn't work and if do the opposite,frame layout doesn't
this is my relative layout
<RelativeLayout 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">
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/mtoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_actionbar">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.rohit.newmastervocab.FloatingFragment"
android:id="#+id/fragment2"
android:layout_alignParentStart="true"
tools:layout="#layout/fragmentfloating"
android:layout_alignTop="#+id/drawer"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
<android.support.v4.widget.DrawerLayout android:id="#+id/drawer1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_below="#+id/toolbar_actionbar"
android:layout_alignParentBottom="true">
<ListView
android:id="#+id/drawerlist1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:entries="#array/navigation_items"
android:background="#F3F6C8"
android:layout_gravity="start">
</ListView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Mike M. is absolutely right: you need to keep FrameLayout as one of the Children of DrawerLayout.
I've updated your XML a bit, take a look:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<FrameLayout
android:layout_below="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_gravity="center_horizontal|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.rohit.newmastervocab.FloatingFragment"
android:id="#+id/fragment2"/>
</FrameLayout>
</RelativeLayout>
<ListView
android:id="#+id/drawerlist1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:entries="#array/navigation_items"
android:background="#F3F6C8"
android:fitsSystemWindows="true"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Now drawer works just as expected and Fragment is stick to the bottom|center of the screen.

Categories

Resources