7

I am trying to add a vertical linear layout (having a title, image, menu title, listview and an icon at the bottom)to the navigation drawer. However the app crashes displaying

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

error. I am not sure if this is possible straight ways as I checked couple of links and the only close I got was this. Any help is appreciated. Thanks in advance. Here is my code.

<!-- The main context view -->

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

<!-- The navigation drawer -->

<LinearLayout        
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:gravity="bottom|center"
    android:orientation="vertical"
    android:paddingTop="30dp" >

    <ListView
        android:id="@+id/left_drawer"                        
        android:choiceMode="singleChoice"
        android:divider="#E8E8E8"
        android:dividerHeight="1dp"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:src="@drawable/footer_image" />
</LinearLayout>

Now the issue here is, xml is not allowing me to define width and height for the listview (Element is unknown) and I am also not able to run it as compiler says "You must supply a layout_width" attribute?? Please help.

My code with the scrollview.

<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" >

<!-- The main context view -->

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

<!-- The navigation drawer -->

<ScrollView
    android:id="@+id/leftRL"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginBottom="5dp"
    android:background="#fff" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="3dp"
                android:src="@drawable/ic_launcher"
                android:text="Dummy"
                android:textColor="#2E3192"
                android:textStyle="bold" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#2E3192" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="2dp" >

                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="3"
                    android:background="#e8e8e8"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_launcher" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="7"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Dummy name"
                        android:textSize="15sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="dummyemail@imhot.com"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:paddingBottom="3dp"
                android:src="@drawable/ic_launcher"
                android:text="Dummy item"
                android:textColor="#2E3192"
                android:textStyle="bold" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#2E3192" />
        </LinearLayout>

        <ListView/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:padding="10dp"
            android:src="@drawable/footer_image" />
    </LinearLayout>
</ScrollView>

</android.support.v4.widget.DrawerLayout>
Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • i believe it should allow you – vipul mittal Feb 07 '14 at 09:28
  • It did, I had to type out the entire thing. Thanks again. – Atul O Holic Feb 07 '14 at 10:09
  • one more issue, i want the entire page to scroll not just the list view so i added Scrollview on the top making the parent linear layout its child as scrollview should have only one child. It compiles, but the output generated shows only 1 item of the list even though there's space on the screen. Added above is my code. – Atul O Holic Feb 08 '14 at 05:55

5 Answers5

13

Your query is difficult to understand without code & stacktrace, but anyway..

Remember android.support.v4.widget.DrawerLayout can have 3 elements only (in exactly this order):

  1. Your main page

  2. Left Drawer

  3. Right Drawer

Copy paste this example(only first two elements are there) & continue with your requirements

<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" >

<!-- Your main screen -->

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/topRL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#DE6D53"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/left_drawer_button"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:onClick="onOpenLeftDrawer"
            android:src="@drawable/ic_launcher" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/frame_to_be_replaced"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/topRL" />
</RelativeLayout>

<!-- left drawer -->

<RelativeLayout
    android:id="@+id/whatYouWantInLeftDrawer"
    android:layout_width="290dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/black" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title"
        android:textColor="@android:color/white" />

    <ListView
        android:id="@+id/left_expandableListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

    <TextView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:textColor="@android:color/white"
        android:text="bottom" />
</RelativeLayout>

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

Your Activity:

public class MainActivity extends Activity {
RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//        I'm removing the ActionBar.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.temp);

    leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    }

    public  void onOpenLeftDrawer(View view)
    {
    drawerLayout.openDrawer(leftRL);
    }
}
Pranav Mahajan
  • 2,048
  • 2
  • 23
  • 36
  • Sorry for missing on the code part. I will make a note of it for future questions. :) And thanks for the answer I will try it and tell you if it works. I guess the only missing thing in my code is the openDrawer() method where I was passing the parameter view itself. – Atul O Holic Feb 07 '14 at 08:47
  • Could you also tell me if I it is possible to create a separate drawer xml and call it in DrawerLayout using ?? – Atul O Holic Feb 07 '14 at 08:53
  • 1
    I haven't tried, but I would avoid that because DrawerLayout widget follows very strict rule of having maximum 3 elements in a page...PS: the just above the left drawer view can be inflated with anything & everything....I can't imagine any scenario apart from that. – Pranav Mahajan Feb 07 '14 at 09:10
  • I meant for the linear layout. However my concern is still about the layout itself, I can ignore the part. Please see my edits in my question I have added the code I am using. – Atul O Holic Feb 07 '14 at 09:12
  • 1
    I think you also like me press "ctrl+space" & expect autofilll to work...but I don't know maybe this issue is with the DrawerLayout widget... in your DrawerLayout xml file you will have to type everything on your own (without autofill).. type this entirely on your own: android:layout_width="match_parent" android:layout_height="match_parent" – Pranav Mahajan Feb 07 '14 at 09:26
6

do 2 things:

  1. set gravity="START" to linearlayout not listview as LinearLayout is now drawer.
  2. at the time of closing the drawer call drawer.close(Gravity.START); don't pass listView object here.
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
5

I got the solution using your answers, thanks :). Actually the issue was with the list view. I was getting the linear layout on the left drawer, however it wasnt displayed properly as I added a scroll View to my linear layout and I also had a list in the same view which displayed only the first item in the list leaving the entire screen space blank. I checked few links to get the answer but it didn't work. At the end, since the list data is known to me and it will not change dynamically I added RadioButtons, customized them and replaced the list.

My code looks like below now.

<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="@color/app_background" >

<!-- The main context view -->

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

<!-- The navigation drawer -->

<ScrollView
    android:id="@+id/leftDrawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:paddingBottom="3dp"
            android:src="@drawable/ic_launcher"
            android:text="@string/menu_account"
            android:textColor="@color/nav_headingsColor"
            android:textStyle="bold" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@color/nav_headingsColor" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imgProfilePic"
                android:layout_width="0dp"
                android:layout_height="70dp"
                android:layout_weight="3"
                android:scaleType="fitXY"
                android:src="@drawable/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/imgProfilePic"
                    android:layout_width="0dp"
                    android:layout_height="70dp"
                    android:layout_weight="3"
                    android:clickable="true"
                    android:contentDescription="@null"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_launcher" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="7"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <TextView
                        android:id="@+id/lblName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Christian Bale"
                        android:textColor="@color/nav_textColor"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/lblEmail"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="christianbale@imhot.com"
                        android:textColor="@color/nav_textColor"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:paddingBottom="3dp"
            android:src="@drawable/ic_launcher"
            android:text="@string/menu_menu"
            android:textColor="@color/nav_headingsColor"
            android:textStyle="bold" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@color/nav_headingsColor" />

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:layout_marginTop="2dip" >

            <RadioButton
                android:id="@+id/radioTopTen"
                style="@style/RadioButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_launcher"
                android:paddingLeft="20dp"
                android:text="@string/menu_topTen" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="@color/nav_itemSep" />

            <RadioButton
                android:id="@+id/radioWish"
                style="@style/RadioButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_launcher"
                android:paddingLeft="20dp"
                android:text="@string/menu_wish" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="@color/nav_itemSep" />

            <RadioButton
                android:id="@+id/radioGifts"
                style="@style/RadioButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_launcher"
                android:paddingLeft="20dp"
                android:text="@string/menu_gifts" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="@color/nav_itemSep" />

            <RadioButton
                android:id="@+id/radioLibrary"
                style="@style/RadioButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_launcher"
                android:paddingLeft="20dp"
                android:text="@string/menu_library" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="@color/nav_itemSep" />

            <RadioButton
                android:id="@+id/radioWhatsOn"
                style="@style/RadioButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_launcher"
                android:paddingLeft="20dp"
                android:text="@string/menu_whatsOn" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="@color/nav_itemSep" />

            <RadioButton
                android:id="@+id/radioDownload"
                style="@style/RadioButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_launcher"
                android:paddingLeft="20dp"
                android:text="@string/menu_download" />
        </RadioGroup>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:padding="10dp"
            android:src="@drawable/footer_image" />
    </LinearLayout>
</ScrollView>

I know this might not be the best possibility but at least its working for me. If any one has a better solution, then please let me know.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
1

This error is coming because of wrong import. Add a proper import for Layout params.

This import "android.widget.LinearLayout.LayoutParams" is getting mixed with Drawerlayout library declaration of LayoutParams ie "android.support.v4.widget.DrawerLayout$LayoutParams" resulting in ClassCast excption.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
Sushil
  • 8,250
  • 3
  • 39
  • 71
  • not sure if this was the case as I have got both the imports. Will still check my luck with this. Thanks 4 the reflex. :) – Atul O Holic Feb 07 '14 at 08:49
0

Try doing this,

In function selectItem, change mDrawerLayout.closeDrawer(mDrawerList); to mDrawerLayout.closeDrawer(mDrawerLinear); Should work perfectly fine.

Because, the reference should now be your linearLayout, its no longer the listview that you are closing or opening.

akash89
  • 881
  • 3
  • 12
  • 31