29

I want to open the drawerlist to the half of the screen for all different device. i tried hard coded values for layout_margineleft 200dp or 100dp to the drawerlist. but it doesn't work in all device it different from device to device. so how can i maintain the exactly half of the screen for drawerlist. i also tried various function like setLeft(int) etc.but some of them doesn't work due to i use minimum version 8. So please help me. thanks in advance.

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);

xml for that:

<?xml version="1.0" encoding="utf-8"?>

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"
 android:layout_height="fill_parent"
 android:layout_margin="@dimen/top_news_linear_margin"
 android:background="@color/news_list_divider_color"
 android:orientation="vertical"
android:padding="@dimen/top_news_linear_padding" >

</RelativeLayout>

<ListView
 android:id="@+id/drawer_list"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:layout_gravity="right"
 android:layout_alignParentTop="true"
 android:background="@color/setting_background"
 android:cacheColorHint="@android:color/transparent"
 android:choiceMode="singleChoice"
 android:divider="@color/news_list_divider_color"
 android:dividerHeight="@dimen/news_list_divider_height"
 />

</android.support.v4.widget.DrawerLayout>
Mayur Raval
  • 3,250
  • 6
  • 34
  • 57

5 Answers5

72

set the width for ListView dynamically...

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);

    int width = getResources().getDisplayMetrics().widthPixels/2;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
    params.width = width;
    mDrawerList.setLayoutParams(params);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • 4
    If your listView is embedded in another layout that is a child of the DrawerLayout, I've found that you set the new layout paramaters on this wrapping layout. Do everything this answer says, but use the listView's parent container if necessary. – jstewart379 Jun 25 '14 at 06:54
  • 2
    am getting class cast exception here..how can i proceed? – GvSharma Apr 07 '15 at 06:38
  • Works in all versions of Android :) – Ajit Jan 26 '16 at 20:24
7

You have to manual calculate the screen size and set dynamic width to the drawer layout at runtime, here is how to get device Screen Width and Height at run time

Using this code you can get runtime Display's Width & Height

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

Now, you need to divide the width by 2

int newWidth=width/2;

Now set width to ListView like this

drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));

This will work for all the devices and will give uniform size across all.

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
5

here is my solution for others who need several percentage to show there navigation drawer.

Here, I use right navigation drawer & i show 15% of home screen when drawer move left.

  // convert your offset percent (here 15% ) into decimal by dividing 100 
    float offset = .15f * getResources().getDisplayMetrics().widthPixels ;
    float width = getResources().getDisplayMetrics().widthPixels - offset;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) rightNavigationView.getLayoutParams();
    params.width = (int) width;
    rightNavigationView.setLayoutParams(params);
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
2

Using the new Android Support Design Library, you can make use of the android.support.percent.PercentRelativeLayout to accomplish this easily. Here's my solution.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="-64dp"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="64dp"/>

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        >

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:fitsSystemWindows="true"
            app:layout_widthPercent="50%"
            app:headerLayout="@layout/nav_header_dashboard"
            app:menu="@menu/menu_drawer_activity_dashboard"/>

    </android.support.percent.PercentRelativeLayout>

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

If you're asking why there's a android:layout_marginRight="-64dp" and android:layout_marginRight="-64dp" in the DrawerLayout and the content. It's because of the fixed margin of 64 dp that is coded directly to the DrawerLayout that wouldn't allow you to use a full widht of the drawer. More about that here

Community
  • 1
  • 1
gentra
  • 6,066
  • 3
  • 16
  • 18
  • This is actually a correct answer. Not because of PercentRelativeLayout, 50% can be obtained in different equally correct ways, but because of -64dp statement, since that's what it causing content movement. However, -64dp has to be corrected in the content area. Otherwise, app content will be shifted to the right. – bajicdusko Dec 23 '16 at 13:06
0
 int currentVerion = Build.VERSION.SDK_INT;
 int width= 0;
 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();

    if(currentVerion >= Build.VERSION_CODES.BASE) {
        display.getSize(size);
        width = size.x;
    }
    else
   {
       width = display.getWidth();
   }

((ListView) view.findViewById(R.id.top_sectionlist)).getLayoutParams().width =width/2;
Ahmad Moussa
  • 1,296
  • 18
  • 22