Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set LayoutParams of ListView when using DrawerLayout with Fragments

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?

like image 858
user2242982 Avatar asked Nov 03 '22 19:11

user2242982


1 Answers

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;
like image 78
Neoh Avatar answered Nov 09 '22 15:11

Neoh