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.
Related
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.
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>
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>
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>
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>