Android DrawerLayout overlays background ListView - android

Got activity with ListView and Navigation Drawer which unfortunately overlays my ListView and I cannot have any interaction like 'onItemSelected' with it.
That's how it looks like:
But
Whenever I change DrawerLayout to not cover whole activity but just
a small piece, .
onitemSelect is working
Below my layout code:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="marcin.chruscicki.notefication.MainActivity"
android:background="#color/universalBackground_DarkGray">
...
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/notesListView"
android:stackFromBottom="false"
android:layout_alignParentStart="true"
android:layout_above="#+id/versionTxtView" />
<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:fitsSystemWindows="true"
>
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#262626" />
</android.support.v4.widget.DrawerLayout>
...
</RelativeLayout>
I cannot find answer anywhere and I am sure this is just a simple thing to fix.

The reason your list view is not working is because u placed it in the bottom layer and over it you placed a drawer so the drawer is over the listview and the drawer is set to match_parent
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="marcin.chruscicki.notefication.MainActivity"
android:background="#color/universalBackground_DarkGray">
...
<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:fitsSystemWindows="true"
>
<!-- The main content view -->
<android.support.design.widget.NavigationView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/nav"/>
</android.support.v4.widget.DrawerLayout>
...
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/notesListView"
android:stackFromBottom="false"
android:layout_alignParentStart="true"
android:layout_above="#+id/versionTxtView" />
</RelativeLayout>
try to place the list view like this when you make a item in the layout it goes one over another that means the last you place the item will be at the front
you need to make the navigation drawer listviw in the fragment layout .Here is a full example how you can dofragment example .hope this will help you.Try to make a fragment for navigation drawer its easy .

I think I just found a solution/workaround for my problem. Instead of putting Drawer Layout between other components like TextView or ListView I used tutorial linked by #neelay-srivastava and created new class + layout with alone DrawerLayout inside. Then changed all my activities to fragments. And it works.
For futher users with same problem just remember to do not nest DrawerLayout in another activity layout between other components.

Related

My emulator shows just Listview on entire screen. even its width is limited to 240dp

I am using drawer layout and Listview is using as drawer.
My emulator just showing Listview as width set to match parent even it's set to 240dp.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer Layout"
android:background="#000000">
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/drawerList"
android:background="#color/colorAccent"/>
<FrameLayout
android:layout_width="250dp"
android:layout_height="match_parent"
android:id="#+id/fragmentContainer"
android:background="#ffffff"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:divider="#android:color/transparent"/>
</android.support.v4.widget.DrawerLayout>
Drawer layouts are usually hidden outside of the screen until you open them. Try opening the drawer by swiping from the side.
You must use NavigationView inside DrawerLayout and limit width in NavigationView.
Example layout structure is like that , there is 2 different usage of NavigationView :
<?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"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- you can add extra views in DrawerLayout -->
<include layout="#layout/content_home"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:menu="#menu/homepage_leftdrawer"/>
<!-- you can use listview, recyclerview etc in navigationview -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
app:headerLayout="#layout/homepage_right_header"
android:layout_gravity="end">
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:layout_marginTop="80dp" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

ScrollView as a first element in Navigation Drawer

I'm trying to create a navigation drawer that looks like google play store. What I've noticed in google play store drawer is that it's scrollable top to bottom (not only the listview). I managed to create a beautiful drawer that opens and closes correctly. But when I use a scrollview as a first element in drawer layout, navigation drawer glitches and shows up always on the screen as a scattered layout.
I use this code (I removed scrollview content for clarity, plus, even just one LinearLayout with 2-3 simple buttons in it, is enough to show the glitch, so it's irrelevant):
<?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">
<!-- Main Layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/screen"
android:layout_height="wrap_content"
tools:context=".Intro"
android:weightSum="1">
</LinearLayout>
<!-- Drawer Layout -->
<ScrollView
android:layout_width="270dp"
android:layout_height="match_parent">
....
....
...
</ScrollView>
Note that I used fixed height elements in the scroll view, and also removed ALL the listviews & expandable list views (views with auto scrolling) without any luck.
I used navigation drawer as following:
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left"
android:name="com.xxx.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
And inside the fragment layout
<LinearLayout 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:orientation="vertical"
android:background="#color/white"
tools:context=".com.xxx.NavigationDrawerFragment">
<ScrollView
android:id="#+id/list_drawer_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

ListFragment.onListItemClick() not being called; possibly due to Navigation Drawer

I am perplexed as to why my onListItemClick() method is not being called.
I was reading this question on the subject, and it mentions that any other components in the layout need to be set android:focusable="false"
However, here is my layout for that class. As you can see, it has no other components:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
I checked in the layout for the parent activity, and it does have some other components, namely a drawer 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"
tools:context=".MainActivity" >
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
<!-- 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">
<!-- 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"/>
</android.support.v4.widget.DrawerLayout>
You can see that my ListFragment is being contained in the FrameLayout called fragment_container.
Could the DrawerLayout be preventing calls to my onListItemClick() method?
And, if so, how can I set is as unfocusable (it's a pretty huge layout...where should I put the android:focusable="false")?
Solution - Set the DrawerLayout as base layout. You do not need to use a RelativeLayout outside of Drawer Layout as it is useless. This might be a hint here
Your layout should be
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
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. -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Recommended to use only one as both are match_parent -->
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/fragment_container"/>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</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>
I discovered the issue. I added a FrameLayout to contain my Fragment, but I see now that the DrawerLayout already contains within it a FrameLayout (called content_frame). So I just used content_frame instead of fragment_container.

Use an Image for DrawerLayout background

I've search on line and here, but I can not find any usefull answer on : "How can I set an Image as a background for my NavigationDrawer's ListView ?"
The problem is whenever my ListView's background isn't a "solid" color (#aabbcc for example), the DrawerLayout doesn't show.
Here's my layout :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/drawer_layout">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#aabbcc"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
/>
[... Rest of main layout ...]
If I change the android:background="#aabbcc" to android:background="#drawable/menu_bg", my app screen just get grey and the Drawer doesn't show.
Solutions I(ve try but doesn't work (same result) :
DrawerLayout background for ListView
Drawer layout has overridden the background
Is it possible to set an Image instead of a single color in a NavigationDrawer ?
Thanks.
What about making the drawer a RelativeLayout and adding an ImageView underneath the ListView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/drawer_layout">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your main layout -->
</FrameLayout>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- The navigation drawer -->
<ImageView
android:id="#+id/drawer_bg"
android:src="#drawable/img2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="#+id/drawer_list"
android:background="#null"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

Android activity with primary and secondary drawers

I am trying to implement a UI similar to that of Google+ where the android activity would have two slide out drawers, the main one from the left and that's the main menu. The secondary one from the right and that's my notifications. However, looking at DrawerLayout I can see that it does not support. Any idea how the Google+ app implements it?
Thanks in advance
I've figured out how to do it using the following layout
<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"/>
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Funny the documentation did not mention this and when the library first came out the DrawerLayout would throw an exception if more than two children existed.
The DrawerLayout does support 2 drawers in a single layout file. Both should get a different value for their layout_gravity attribute ("left" and "right").
Example snippet:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Main content -->
</LinearLayout>
<FrameLayout
android:id="#+id/nav_left"
android:layout_width="#dimen/nav_left_width"
android:layout_height="match_parent"
android:layout_gravity="left">
<!-- Content of your left drawer -->
</FrameLayout>
<FrameLayout
android:id="#+id/nav_right"
android:layout_width="#dimen/nav_right_width"
android:layout_height="match_parent"
android:layout_gravity="right">
<!-- Content of your right drawer -->
</FrameLayout>
</android.support.v4.widget.DrawerLayout>

Categories

Resources