2

I am trying to add a Navigation Drawer inside my app. Here's the code:

<DrawerLayout 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="com.example.abcxyz.properprojectdb.MyDrawerActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:entries="@array/planets"></ListView>
</DrawerLayout>

I am unable to add the attribute android:layout_gravity for ListView. I don't know why it is not available for use (intellisense works perfectly, but it won't show layout_gravity)

My app crashes when I run it, but if I replace DrawerLayout with LinearLayout, the app doesn't crash. I am guessing that it is because I have not added the attribute android:layout_gravity for ListView. enter image description here Here's a snapshot to show that layout_gravity doe not appear in intellisense either

sga4
  • 350
  • 4
  • 15

3 Answers3

13

I had the same issue and I wasted maybe 2 hours to find a solution. I tried restarting the IDE, invalidating, downgrading from 23 to 22, nothing worked.

Eventually, I HARD TYPEd it and it became available after I finished typing

android:layout_gravity=

(Worth mentioning I was on AS 2.1 ... maybe there was a weird bug)

DoruChidean
  • 7,941
  • 1
  • 29
  • 33
  • 1
    this is the right answer , it seems that android studio doesn't autocomplete this attribute , no matter which version of v4 support lib your using , all you have to do is just type all of "android:layout_gravity=" in you layout editor – Mahdi Javaheri Apr 05 '17 at 05:41
  • 1
    Thanks, it worked. android studio made us fond of auto completion. – Nishant Bhakta Jul 23 '17 at 14:18
4

Replace: DrawerLayout with android.support.v4.widget.DrawerLayout

and add following line to your build.gradle:

compile "com.android.support:support-v4:22.0.0"

UPDATE

DrawerLayout should consist of 2 views and it seems that you cannot set layout_gravity to the view which represents drawer itself. You can always wrap your ListView in other layout and then set layout_gravity on your ListView.

questioner
  • 2,283
  • 3
  • 26
  • 35
  • I did as you said.. when I tried to build it, gradle couldn't find 'com.android.support:support-v4:22.0.0', so I added 'com.android.support:support-v4:21.+' instead. The app won't crash now when I run it, BUT layout_gravity still won't appear in intellisense – sga4 Apr 12 '15 at 16:49
  • true! but it's weird. As I run this app, the drawer appears and won't slide in/out on swiping.. And even in the documentation, https://developer.android.com/training/implementing-navigation/nav-drawer.html there is layout_gravity for ListView not wrapped inside another layout. – sga4 Apr 12 '15 at 17:21
  • 1
    I don't know from where it took the u-turn, but all of a sudden, it's working!! Without having to wrap the ListView inside another layout, it's working! I wrote layout_width ignoring the intellisense, built the app, and it complained. Built it again, and it worked! – sga4 Apr 12 '15 at 17:47
1

Every things seem to be no problem. You can put android:layout_gravity="start" or android:layout_gravity="end" in ListView

UPDATE

I saw my bad. @questioner was right. Your must change DrawerLayout to android.support.v4.widget.DrawerLayout

Robust
  • 2,415
  • 2
  • 22
  • 37
  • But layout_gravity doesn't exist in my project. Does it have something to do with appcompat libraries? In the tutorial that I am following (slidenerd), they add an appcompat v4 dependency which I have not added. Could it be because of that? (I've tried to add it, but it might not have been added properly) – sga4 Apr 12 '15 at 16:22
  • @sga4 As said in my answer - you have to use support library and DrawerLayout from this support library because it doesn't exist elsewhere – questioner Apr 12 '15 at 16:30
  • oh, sorry, i forgot that. what is you IDE? Android Studio or Eclipse? – Robust Apr 12 '15 at 16:32