How to customize NavigationDrawer - java

I have the seguent NavigationDrawer and it is ok:
<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"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_gravity="start"
android:background="#FFF"
android:choiceMode="singleChoice"
android:layout_height="match_parent">
</ListView>
The problem is born when I try to customize my navigationDrawer, It should insert a "static" Text in my ListView (like a TextView) not like in adapter.

Related

How do I make the background of my navigation drawerlayout for it to be the activity_main.xml in xml in android studio?

I am trying to create a separate page for my drawerlayout and add the background as the activity_main.xml so that the two pages are separate but I can't manage to do this. How do I do this in xml in android studio? This is my code for my drawerlayout page in xml in android studio. What changes do I have to make for this to work?
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:elevation="4dp"
android:background="#6666ff"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:background="#+id/main_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/navig_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

My background is under navbar.How to set the background just below the navbar?

My background hides under the navbar.
How can I put the background start showing just below the navbar?
code:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/backgroundnew"
android:scaleType="centerCrop" />
In your activity_main.xml, Have you closed the toolbar correctly?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
If you didn't close your toolbar, the view is overlapped.

Prevent FAB from going down when scrolling in RecyclerView in fragment

I'm new to android development and recently learning about fragments.
I have a recyclerview inside fragment, and I want it to be attached to the toolbar, I tried put app:layout_behavior="#string/appbar_scrolling_view_behavior"
inside the container layout but whenever the toolbar shows the FAB moves down below the navigation bar as you can see here.
activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/main_screen"
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="com.atar.mango.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/toolbar"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/logo_header"
app:menu="#menu/menu_for_navigation_view"/>
</android.support.v4.widget.DrawerLayout>
home_fragment.XML
<android.support.design.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"
tools:context="com.atar.mango.HomeFragment"
android:background="#color/background">
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/empty"
android:gravity="center"
android:textSize="90sp"
android:textColor="#32000000"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/notes"
app:backgroundTint="#color/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_note"
app:backgroundTint="#color/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="#dimen/fab_margin_right"
android:layout_marginEnd="#dimen/fab_margin_right"
app:layout_anchorGravity="bottom|right|end"
android:layout_marginBottom="16dp"
android:src="#mipmap/add_note"
app:borderWidth="0dp"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:fabSize="normal"/>
</android.support.design.widget.CoordinatorLayout>
Thanks.
I think you must specify your layout target,
in this way:
<android.support.design.widget.FloatingActionButton
...
android:layout_gravity="bottom|end|right"
app:layout_anchor="#id/notes"
app:layout_anchorGravity="bottom|right|end"
/>

Recycler View is not scrolling in Navigation drawer

I have created a navigation drawer and in the main content of navigation drawer I have a RecyclerView but it's not scrolling. Initially I have put RecyclerView without NestedScrollView but that didn't work, so I tried putting it inside NestedScrollView but the latter didn't work also.
My navigation drawer XML is :
<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="com.roushan.assignment1.NavDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/nav_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/nav_toolbar_color"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!-- MAIN CONTENT OF NAVIGATION DRAWER ACTIVITY -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
tools:openDrawer="start">
<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_drawer_header"
app:menu="#menu/activity_nav_drawer_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
As Mike pointed out , the way you have structured the layout is wrong. Please modify the layout as below:
<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="com.roushan.assignment1.NavDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/nav_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/nav_toolbar_color"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
tools:openDrawer="start">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<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_drawer_header"
app:menu="#menu/activity_nav_drawer_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

How use two fragment in Android DrawerLayout

I try to use Android NavigationDrawe - http://developer.android.com/training/implementing-navigation/nav-drawer.html
I have one activity and two fragments.
My main problem is layout in android.support.v4.widget.DrawerLayout.
One fragment use -content_frame and other - content_footer. This element would be in bottom (height - wrap_content). I have tried different variations, but did not work. All examples is with one fragment. What I am doing wrong? thank you
My main layout file. With this show one fragment.
<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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
>
</FrameLayout>
<FrameLayout
android:id="#+id/content_footer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/abs__bright_foreground_disabled_holo_light"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
When change to:
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
>
</RelativeLayout>
<RelativeLayout
android:id="#+id/content_footer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
>
</RelativeLayout>
</FrameLayout>
shows both fragments, but on one another. screenshot
If I understand your problem correctly, you want to have a fragment at the top, and you want the second fragment as a footer on the screen. It's unclear if you want scrolling or not, so I'll just assume not. The DrawerLayout doesn't actually do much in terms of laying out elements to my knowledge, so I'd recommend putting a layout inside the DrawerLayout to position your fragments as you want. Do something similar to the untested code I'll put below:
<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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80" >
</FrameLayout>
<FrameLayout
android:id="#+id/content_footer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20" >
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/abs__bright_foreground_disabled_holo_light"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Best of luck! Hope that helps!

Categories