34

I would like to have for example a LinearLayout or a RelativeLayout sliding from the left of the screen instead of a lone ListView.

I tried to use à LinearLayout with android:layout_gravity="start" and i had this error at runtime:

ClassCastException: android.widget.LinearLayout$LayoutParams cannot 
be cast to android.support.v4.widget.DrawerLayout$LayoutParams

here's the layout file:

<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="@android:color/white"
    >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout 
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ImageView 
            android:id="@+id/ivwLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/video_icon"
        />

        <ListView 
            android:id="@+id/left_drawer"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@android:color/white"
        />

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

Thanks

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
BigDan
  • 517
  • 2
  • 6
  • 12
  • How are you referencing your DrawerLayout in code ? Can you maybe post that. – Marco RS Jun 07 '13 at 00:12
  • 2
    For others running into the `android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams` problem: **[this question](http://stackoverflow.com/questions/17939798/classcastexception-android-widget-framelayoutlayoutparams-to-android-support-v4)** directly answers that. – Jonik Oct 30 '13 at 15:42

4 Answers4

63

This will work if you move both the android:id="@+id/left_drawer" (or create a new id) and set the gravity.

The id move (or new one) is so the reference is correct so you call closeDrawer() on it and not the child views.

But most importantly, the DrawerLayout requires that element to have a android:layout_gravity set on it, as you mentioned.

Finally, you need to call close closeDrawer() on the base view, the one with the required gravity.

Example:

<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="@android:color/white">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout 
        android:id="@+id/left_drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ImageView 
            android:id="@+id/ivwLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/video_icon" />

        <ListView 
            android:id="@+id/left_drawer_child"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@android:color/white" />

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

In code:

DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout)
LinearLayout mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
ListView mDrawerListChild = (ListView) findViewById(R.id.left_drawer_child);

...

mDrawer.closeDrawer(mDrawerLinear);

(This is basically what @Karakuri posted, but with a more complete explanation and example.)

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
26

Yes it is possible to have any view as the sliding part of a drawer layout. I prefer declaring a FrameLayout as the drawer and replacing it with my fragment, and it runs just fine.

The error you are getting is probably due to some other reason in the Java part of your implementation.

Sam
  • 3,413
  • 1
  • 18
  • 20
  • Thanks for the reply but, if I remove the linearlayout and the imageview to let the listview alone, I have no error and it work well. so I would be surprise that the error is in the java code. – BigDan May 24 '13 at 20:30
  • Mind posting the code and logcat anyway? otherwise can't think of a reason what might be going wrong. – Sam May 24 '13 at 20:34
  • 10
    Finaly you were rigth, the error was in java. I had to change: mDrawerLayout.closeDrawer(mDrawerList); for mDrawerLayout.closeDrawer(mDrawerView); in the code where mDrawerView is the linearlyout instead of the list. Thank you very much for your help – BigDan May 25 '13 at 13:47
  • i am having an issue with this, i mean i have the framelayout and a fragment in it, but then i get an error saying that the framelayout is not a sliding drawer, is thre any thing i am missing? Please assist @SamarthJain – Rat-a-tat-a-tat Ratatouille Mar 25 '14 at 09:10
  • 2
    oh okie, i got it in the comments in the below answer, layout_gravity="start" is what i was missing! – Rat-a-tat-a-tat Ratatouille Mar 25 '14 at 09:12
  • @samarth I'm attempting to do what you described in your answer, I use a FrameLayout and use the fragmentmanager to load a listfragment into the drawer. However, I'm having trouble with scrolling in my listFragment being interrupted by the drawer. Can you help?http://stackoverflow.com/questions/23838120/vertical-scrolling-interrupted-by-oversensitive-drawerlayout-when-using-drawerla – android_student May 23 '14 at 21:19
  • @SamarthJain u can use replace mDrawerLayout.closeDrawer(mDrawerList); this code with :mDrawerLayout.closeDrawer(linearlayoutID); and it will load all u r layout into this. – nileshbirhade Nov 07 '14 at 09:58
7

Make sure you pass correct object (your LinearLayout called mDrawerLinear) for methods like isDrawerOpen, closeDrawer etc. This line solved my ClassCastException:

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear);
Roman
  • 860
  • 14
  • 14
2

Try moving android:id="@+id/left_drawer" to the LinearLayout instead of the ListView

Karakuri
  • 38,365
  • 12
  • 84
  • 104