Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set navigation drawer list to open exact half of the screen for all device screen

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> 
like image 413
Mayur Raval Avatar asked Jan 10 '14 07:01

Mayur Raval


People also ask

How do I customize my navigation drawer?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

How do I get navigation drawer in all activity?

The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.

What is Android drawer layout?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.


2 Answers

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); 
like image 183
Gopal Gopi Avatar answered Sep 20 '22 14:09

Gopal Gopi


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.

like image 41
Akhil Jain Avatar answered Sep 18 '22 14:09

Akhil Jain