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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With