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>
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.
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):
Your main page
Left Drawer
Right Drawer
Copy paste this example(only first two elements are there) & continue with your requirements
Your Activity:
do 2 things:
gravity="START"
to linearlayout not listview as LinearLayout is now drawer.drawer.close(Gravity.START);
don't pass listView object here.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.
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.
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.