Android Navigatoin Drawer (Menu) with scrollable footer - android

I want to implement a Navigation Drawer (left menu) in Android.
The menu should have a list of items at the top and a footer with two buttons on the bottom.
I have taken two approaches:
1) Add a footer to the list via
mDrawerList.addFooterView(footer);
With this solution the footer is like another item of the list. So if the list has few items, then the footer doesn't show in the bottom, in fact it is right below the last list item.
2) Apply a Relative Layout to the left component like:
<?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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:id="#+id/left_drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:background="#color/black"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="20dp">
<Button
android:id="#+id/dashboard_iv_profile_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="Test"/>
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
However when not all the menu list items are shown the footer is above the list items, hidding the last items.
How can I achieve the right solution?
Do I have to use a ScrollView?
Thanks!
Edited 1
Using the 2nd approach and the android:layout_above in the left_drawer RelativeLayout I get something similar to the picture taken:

It looks to me like there is an issue with your layout. You shouldn't need two RelativeLayouts.
Try something like this:
<RelativeLayout
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:id="#+id/left_drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:layout_above="#+id/dashboard_iv_profile_image"
android:background="#color/black"/>
<Button
android:id="#+id/dashboard_iv_profile_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_margin="20dp"
android:text="Test"/>
</RelativeLayout>
Notice the addition of layout_above in the ListView and moving the alignParentBottom to the Button.

Related

Navigation Drawer Layout overlapping with main layout

How do I get my main layout to simply line up directly underneath my DrawerLayout? My main layout is listed below and is mainly made up of listviews. Right now everything is overlapping. So the below first shows my DrawerLayout. After closing of DrawerLayout my main activity is listed.
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name=“com.example.my.app.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent”
android:orientation="vertical">
<TextView
android:id="#+id/txt_empty_list_cars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="4dp"
android:gravity="center"
android:text="#string/view1"
android:textAppearance="?android:attr/textAppearance"
android:textColor="#android:color/darker_gray"
android:visibility="gone" />
<ListView
android:id="#+id/list_cars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_header"
android:layout_margin="4dp"
android:divider="#android:drawable/divider_horizontal_bright"
android:dividerHeight="0.5dp"
android:listSelector="#android:drawable/list_selector_background" />
So, your problem is drawer layout overlapping the linear layout below. Use
android: layout_below="#id/drawer_layout"
In your linear layout. That will linearly arrange both layouts.
I dont know if your are looking for a sliding panel like the new gmail app
but if yes you can refer yourself Sliding Pane Layout and this very awesome tutorial.
hope it helps, happy codings.
In order to use the DrawerLayout correctly, it has to be the only root / parent view of your entire layout.
<android.support.v4.widget.DrawerLayout>
/*other nested child views */
<fragment
android:id="#+id/fragment_navigation_drawer"
...
/>
</android.support.v4.widget.DrawerLayout>
Also, to prevent the over lapping, ensure that you have two direct child views as per design guideline at
https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
"To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent . Add drawers as child views after the main content view and set the
layout_gravity appropriately."

Add navigation drawer to content view containing ListView and Button

I have a view layout containing a ListView and a Button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#5434"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="1.0"
android:id="#+id/itemlistView" tools:ignore="ObsoleteLayoutParam"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
tools:ignore="ObsoleteLayoutParam">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/button"
android:text="#string/new_item"
android:id="#+id/listview_addDebtButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
tools:ignore="ObsoleteLayoutParam"/>
</LinearLayout>
but now i want to add a Navigation Drawer. Using the sample code from google developers. But am not sure how to do this since the sample code contains an empty content view:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
I however proceeded to try with this as my new layout:
<?xml version="1.0" encoding="utf-8"?>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:id="#+id/itemlistView" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/button"
android:text="#string/new_item"
android:id="#+id/listview_addDebtButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
/>
</LinearLayout>
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
This fails woefully when I try to run the app and i get a android.view.InflateException with the error message:
error inflating class DrawerLayout. Am not sure how to proceed from here. "There is a similar question that asks "How do I add navigation drawer to my existing code?" but this problem is way too complicated than mine and I can't apply the solutions to my problem. Any suggestions will be appreciated.
Try like this:
According to docs: Drawer Layout tooks two direct children
Inside the DrawerLayout, add one view that contains the main content
for the screen (your primary layout when the drawer is hidden) and
another view that contains the contents of the navigation drawer.
From that reference I used one for FrameLayout and another one for ListView.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ListView
android:id="#+id/itemlistView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="1.0"
tools:ignore="ObsoleteLayoutParam" />
<Button
android:id="#+id/listview_addDebtButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
android:background="#drawable/button"
android:text="#string/new_item"
tools:ignore="ObsoleteLayoutParam" />
</RelativeLayout>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

Can't click the buttons behind the DrawerLayout

here is my xml:
<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"
tools:context=".Main"
android:background="#android:color/black" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/about"
android:layout_centerHorizontal="true"
android:background="#drawable/calendar" />
<ImageButton
android:id="#+id/okan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/calendar"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/calendar"
android:background="#drawable/okanliyiz"/>
<ImageButton
android:id="#+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:layout_toLeftOf="#+id/calendar"
android:background="#drawable/info" />
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
I wrote the programmatic part of it and the Drawer works just fine, it's just that I cannot click the ImageButtons behind it. If I put the buttons in front I am able to click them but well, then the drawer is left behind and that's a terrible sight. What is the work-around for this? How can I both click the buttons behind the Drawer and see the Drawer in the front?
Thanks in advance.
According to the Navigation Drawer guide the DrawerLayout should be the root of your layout. It should have only 2 children - one that contains your "main content" - buttons, text fields, etc. And the other one should be the content of the drawer itself. Something like this:
<android.support.v4.widget.DrawerLayout>
<RelativeLayout>
<Button/>
<EditText/>
</RelativeLayout>
<ListView android:id="#+id/drawer_list" />
</android.support.v4.widget.DrawerLayout>
In addition:
The order of the 2 children is important due to the Z-order of the DrawerLayout (which is a ViewGroup). The list view should be declared after your main content so that it's ordered(and displayed) in front of it.

Android ProgressBar is not aligning in center of layout

I am using a progress bar inside drawer layout. When I start the app the progress bar is always aligned to top left corner of screen. I tried to set android:layout_gravity property but still It is shown in top left corner. Below is my xml layout file.
<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:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
style="?android:attr/progressBarStyleLarge" />
<ListView
android:id="#+id/mainListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:dividerHeight="0dp"
android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>
use android:gravity="<whatever>" on the FrameLayout, since you only have the progressbar inside it.
Also, your drawer is going to be the listview because that's what has the layout_gravity="start" property set. If you want the progress bar to be within the left-drawer you can wrap the progress bar and listview into a container and set the layout_gravity="start" on that.
without further explanation, that's all i can aid with.
It's work for me. In FrameLayout set
android:layout_centerInParent="true"
Please try this code, This code is work for me.
ProgressBarView.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_gravity="center"
android:orientation="vertical">
</ProgressBar>
Now include this layout into NavigationView,
<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">
<include layout="#layout/progress_bar"/>
</android.support.design.widget.NavigationView>
Preview in Studio

How to push included layout to bottom of screen?

I have a layout called footer.xml which later will be included to multiple layouts. Footer.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottomMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="#drawable/bg_gradation"
android:weightSum="1" >
*** STUFF GOES HERE ***
</LinearLayout>
Footer.xml will then be included in other layout as follows..
<?xml version="1.0" encoding="utf-8"?>
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backrepeat">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="0dp"
android:weightSum="1">
<ListView
android:id="#+id/chatListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:divider="#00000000"
android:scrollbars="none"
android:dividerHeight="1dp"
android:layout_weight=".80"/>
<LinearLayout
android:id="#+id/chatBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".13"
android:orientation="horizontal"
android:background="#242424"
android:weightSum="1">
</LinearLayout>
</LinearLayout>
<include layout="#layout/footer"/>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Since i want footer.xml be at the bottom screen, i use android:layout_gravity="bottom" this renders properly if seen from the perspective of footer.xml
However, when i try to see it from other layouts it throws error that says:
java.lang.UnsupportedOperationException (Error Log does not show helpful information at all).
EDIT 1
This is the best error message i could get.. (i cannot copy-paste the log text)
EDIT 2
IF i include my layout this way:
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/footer"
android:id="#+id/footer"/>
</LinearLayout>
It renders the way i want it inside eclipse editor. However, when i try to run it on Android i get the following error:
java.lang.IllegalStateException: Child android.widget.LinearLayout{a72d4938 V.E..... ......ID 0,0-0,0 #7f0a005c app:id/bottomMenu} at index 1 does not have a valid layout_gravity - must be Gravity.LEFT, Gravity.RIGHT or Gravity.NO_GRAVITY
and if i editted my footer.xml to this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottomMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" <---- not android:layout_gravity="bottom"
android:background="#drawable/bg_gradation"
android:weightSum="1" >
my footer would fill up the entire screen from top to bottom.
Can anyone help me with this? Thanks..
Put you linearLayout and footer inside relative layout
<include
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/activity_main" />
XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backrepeat">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content_frame"
android:layout_above="#+id/footer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="0dp"
android:weightSum="1">
<ListView
android:id="#+id/chatListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:divider="#00000000"
android:scrollbars="none"
android:dividerHeight="1dp"
android:layout_weight=".80"/>
<LinearLayout
android:id="#+id/chatBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".13"
android:orientation="horizontal"
android:background="#242424"
android:weightSum="1">
</LinearLayout>
</LinearLayout>
<include layout="#layout/footer" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Categories

Resources