5

I want to change width and height of my Listview which is located in my DrawerLayout:

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
DrawerLayout.LayoutParams myParam = new DrawerLayout.LayoutParams(20, 20);
mDrawerList.setLayoutParams(myParam);

But I get this error:

06-21 09:30:57.361: E/AndroidRuntime(12583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jk.demo/com.jk.demo.MemoStart}:
  java.lang.IllegalArgumentException: View android.widget.ListView{414506a8 VFED.VC. ......I. 0,0-0,0 #7f04003a app:id/left_drawer} is not a sliding drawer

drawer_main.xml

<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/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="540dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

I imported:

import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.LayoutParams;

Any solutions?

cringe
  • 13,401
  • 15
  • 69
  • 102
user2242982
  • 61
  • 1
  • 5
  • I am running into the same issue even with ListView.LayoutParams: `ListView.LayoutParams myParam = new ListView.LayoutParams(20, 20);` This is my error: `06-30 20:57:30.431: E/AndroidRuntime(18212):` `Caused by: java.lang.IllegalArgumentException:` `View android.widget.ListView{4271e6f0 V.ED.VC. ......I. 0,` `0-0,0 #7f040035 app:id/left_drawer} is not a sliding drawer` – AG1 Jul 01 '13 at 04:04

2 Answers2

2

Without creating a new layoutParam object, you can just set the width/height of the listview like this

mDrawerList.getLayoutParams().width = 20;
mDrawerList.getLayoutParams().height = 20;

Note that 20 is in pixel. If you want to set (usually) in dp value, then you need to do some conversion

int widthDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
int heightDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
mDrawerList.getLayoutParams().width = widthDp;
mDrawerList.getLayoutParams().height = heightDp;
Neoh
  • 15,906
  • 14
  • 66
  • 78
0

if you use a custom adapter for list view you can inflate an suitable picture in last item.
in order to do not miss last item you can add an invalid data to list data

Hossein
  • 314
  • 3
  • 12