Full width Navigation Drawer
Asked Answered
A

16

43

I'd like to create a full width navigation drawer. Setting layout_width to match_parent on @+id/left_drawer yields in width of about 80% of screen space. This seems to be the standard behavior. Do I have to override onMeasure() of DrawerLayout?

My current code:

<?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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:id="@+id/mainFragmentContainer">
    </FrameLayout>

    <include
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/drawer"/>
</android.support.v4.widget.DrawerLayout>

Thanks.

Aspirant answered 25/5, 2013 at 21:59 Comment(0)
G
31

Yes, you have to extend DrawerLayout and override some methods because MIN_DRAWER_MARGIN is private

Here is a possible solution:

public class FullDrawerLayout extends DrawerLayout {

    private static final int MIN_DRAWER_MARGIN = 0; // dp

    public FullDrawerLayout(Context context) {
        super(context);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalArgumentException(
                    "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }

        setMeasuredDimension(widthSize, heightSize);

        // Gravity value for each drawer we've seen. Only one of each permitted.
        int foundDrawers = 0;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE) {
                continue;
            }

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (isContentView(child)) {
                // Content views get measured at exactly the layout's size.
                final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
                        widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
                final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
                        heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
                child.measure(contentWidthSpec, contentHeightSpec);
            } else if (isDrawerView(child)) {
                final int childGravity =
                        getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
                if ((foundDrawers & childGravity) != 0) {
                    throw new IllegalStateException("Child drawer has absolute gravity " +
                            gravityToString(childGravity) + " but this already has a " +
                            "drawer view along that edge");
                }
                final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                        MIN_DRAWER_MARGIN + lp.leftMargin + lp.rightMargin,
                        lp.width);
                final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                        lp.topMargin + lp.bottomMargin,
                        lp.height);
                child.measure(drawerWidthSpec, drawerHeightSpec);
            } else {
                throw new IllegalStateException("Child " + child + " at index " + i +
                        " does not have a valid layout_gravity - must be Gravity.LEFT, " +
                        "Gravity.RIGHT or Gravity.NO_GRAVITY");
            }
        }
    }

    boolean isContentView(View child) {
        return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
    }

    boolean isDrawerView(View child) {
        final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
        final int absGravity = Gravity.getAbsoluteGravity(gravity,
                child.getLayoutDirection());
        return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
    }

    int getDrawerViewGravity(View drawerView) {
        final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
        return Gravity.getAbsoluteGravity(gravity, drawerView.getLayoutDirection());
    }

    static String gravityToString(int gravity) {
        if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
            return "LEFT";
        }
        if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
            return "RIGHT";
        }
        return Integer.toHexString(gravity);
    }

}
Gladsome answered 5/8, 2013 at 20:22 Comment(7)
For anyone with little knowledge of the source code of DrawerLayout, this answer may seem like demon magic conjured in the head of a wizard, and make you want to give up on programming. Fret not! This is simply the source code of DrawerLayout with the only methods necessary taken out. The only thing that really changes here is that "MIN_DRAWER_MARGIN = 64;" was changed to "MIN_DRAWER_MARGIN = 0;". Another idea to extend upon this would be to instead make MIN_DRAWER_MARGIN an extendable xml attribute.Suspension
Now that I think about it, removing this MIN_DRAWER_MARGIN field entirely already allows users to modify this through the layout_margin* fields, or to not specify it at all which will default to 0dp.Suspension
Replace getLayoutDirection with ViewCompat.getLayoutDirection to get more compatibility. Anyway this doesn't work for me on android 7.0. Margin isn't changed and actionbar now covers status bar.Waistcloth
Getting Fatal Exception: java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true in onMeasure()Kiki
I've tried this and working, but now my whole activity layout gets over under status bar. I set fitsSystemWindows to true but not working. HelpAnelace
To me the best answer is the third one most rated. It's very precise setting the navigation-view width the same as the window in pixels, and doesn't require a lot of code.Punctual
@Taufik Nur Rahmanda just set fitsSystemWindows to false. It won't show under status bar againPachysandra
R
133

If you want simpler solution you can just set negative margin

android:layout_marginLeft="-64dp"

for your left_drawer:

<include
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/drawer"
        android:layout_marginLeft="-64dp"/>
Rigsby answered 30/10, 2013 at 9:18 Comment(7)
I think this should have been the accepted answer. I have looked up the source code. It does not take the 80% percent of the space. It just put minimum of 64dp margin. Putting negative margin is wise.Mitchmitchael
Works great. I noticed that there was still 1 pixel of a gap so 65dp works for meBrig
simple without any coding, 65dp is bestDistasteful
This solution not working with Android 5.0 and above :(Morbilli
Worked on 5.0 and above.Focal
Is that on the sw600dp? for instance ? I want to be able to set the navigation drawer full on my tablet screen. So far it appears half and I can't see where to alter and change the setting of it. @RigsbyCortese
i set it to 10 dp not -10dp and gives me expected result. Thanks.Chelyuskin
G
31

Yes, you have to extend DrawerLayout and override some methods because MIN_DRAWER_MARGIN is private

Here is a possible solution:

public class FullDrawerLayout extends DrawerLayout {

    private static final int MIN_DRAWER_MARGIN = 0; // dp

    public FullDrawerLayout(Context context) {
        super(context);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalArgumentException(
                    "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }

        setMeasuredDimension(widthSize, heightSize);

        // Gravity value for each drawer we've seen. Only one of each permitted.
        int foundDrawers = 0;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE) {
                continue;
            }

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (isContentView(child)) {
                // Content views get measured at exactly the layout's size.
                final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
                        widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
                final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
                        heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
                child.measure(contentWidthSpec, contentHeightSpec);
            } else if (isDrawerView(child)) {
                final int childGravity =
                        getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
                if ((foundDrawers & childGravity) != 0) {
                    throw new IllegalStateException("Child drawer has absolute gravity " +
                            gravityToString(childGravity) + " but this already has a " +
                            "drawer view along that edge");
                }
                final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                        MIN_DRAWER_MARGIN + lp.leftMargin + lp.rightMargin,
                        lp.width);
                final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                        lp.topMargin + lp.bottomMargin,
                        lp.height);
                child.measure(drawerWidthSpec, drawerHeightSpec);
            } else {
                throw new IllegalStateException("Child " + child + " at index " + i +
                        " does not have a valid layout_gravity - must be Gravity.LEFT, " +
                        "Gravity.RIGHT or Gravity.NO_GRAVITY");
            }
        }
    }

    boolean isContentView(View child) {
        return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
    }

    boolean isDrawerView(View child) {
        final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
        final int absGravity = Gravity.getAbsoluteGravity(gravity,
                child.getLayoutDirection());
        return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
    }

    int getDrawerViewGravity(View drawerView) {
        final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
        return Gravity.getAbsoluteGravity(gravity, drawerView.getLayoutDirection());
    }

    static String gravityToString(int gravity) {
        if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
            return "LEFT";
        }
        if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
            return "RIGHT";
        }
        return Integer.toHexString(gravity);
    }

}
Gladsome answered 5/8, 2013 at 20:22 Comment(7)
For anyone with little knowledge of the source code of DrawerLayout, this answer may seem like demon magic conjured in the head of a wizard, and make you want to give up on programming. Fret not! This is simply the source code of DrawerLayout with the only methods necessary taken out. The only thing that really changes here is that "MIN_DRAWER_MARGIN = 64;" was changed to "MIN_DRAWER_MARGIN = 0;". Another idea to extend upon this would be to instead make MIN_DRAWER_MARGIN an extendable xml attribute.Suspension
Now that I think about it, removing this MIN_DRAWER_MARGIN field entirely already allows users to modify this through the layout_margin* fields, or to not specify it at all which will default to 0dp.Suspension
Replace getLayoutDirection with ViewCompat.getLayoutDirection to get more compatibility. Anyway this doesn't work for me on android 7.0. Margin isn't changed and actionbar now covers status bar.Waistcloth
Getting Fatal Exception: java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true in onMeasure()Kiki
I've tried this and working, but now my whole activity layout gets over under status bar. I set fitsSystemWindows to true but not working. HelpAnelace
To me the best answer is the third one most rated. It's very precise setting the navigation-view width the same as the window in pixels, and doesn't require a lot of code.Punctual
@Taufik Nur Rahmanda just set fitsSystemWindows to false. It won't show under status bar againPachysandra
O
31

Because all these answers did not work on OS 6.0.1, I'll post here the solution that worked for me in combination with DrawerLayout + NavigationView.

So all what I do is change the width of the NavigationView programatically:

mNavigationView = (NavigationView) findViewById(R.id.nv_navigation);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mNavigationView.getLayoutParams();
params.width = metrics.widthPixels;
mNavigationView.setLayoutParams(params);

This works for all screen sizes.

Onto answered 22/12, 2015 at 12:29 Comment(3)
i do not see where half the screen is being set to the width (also I tried params.width = metrics.widthPixels/2) but it is not doing anything no matter what I change the width toEtymologize
this solution working in 2019. I tested in Android versions 22, 26, 28 I like it because is precise to set the navigation view to the full width of the window. I don't like setting a fixed negative right padding.. odd this is not the top rated answer.Punctual
This is the working solution rather than setting negative margins. Thanks.Selfabsorbed
P
16

Based on the Robert's Answer, you can use the layout_marginLeft=-64dp to solve this problem easily.

However it doesn't seems to work anymore on Android 5.0 and above. So here's my solution that worked for me.

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

    <include
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/drawer"/>

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

Basically, Add android:layout_marginRight="-64dp" to the root DrawerLayout so all the layout will go to the right for 64dp.

Then I add the layout_marginRight=64dp to the content so it goes back to the original position. Then you can have a full drawer there.

Pergola answered 8/2, 2016 at 2:48 Comment(1)
this is a nice workaround! I would suggest to mark it as correct answerMinos
H
11

A variant on Grogory's solution:

Instead of subclassing I call the following utility method right after I grab a reference to the drawer layout:

/**
 * The specs tell that
 * <ol>
 * <li>Navigation Drawer should be at most 5*56dp wide on phones and 5*64dp wide on tablets.</li>
 * <li>Navigation Drawer should have right margin of 56dp on phones and 64dp on tablets.</li>
 * </ol>
 * yet the minimum margin is hardcoded to be 64dp instead of 56dp. This fixes it.
 */
public static void fixMinDrawerMargin(DrawerLayout drawerLayout) {
  try {
    Field f = DrawerLayout.class.getDeclaredField("mMinDrawerMargin");
    f.setAccessible(true);
    f.set(drawerLayout, 0);

    drawerLayout.requestLayout();
  } catch (Exception e) {
    e.printStackTrace();
  }
}
Hohenzollern answered 17/12, 2014 at 5:6 Comment(3)
I am not sure if its the right way but very clever solutionEsmond
It's not 100% correct. Ideally you would never call printStackTrace in production and you would cache f in a static field so you do the reflection lookup just once.Hohenzollern
Thanks. Good variant of reflection use. When worked with App branding feature used it many times to cover similar cases for colours and other UI properties.Tizzy
G
4

Nipper's FullDrawerLayout Class is just simply awesome.. it's performance is also faster than the default drawer how ever you can;t use it on devices with api that don't have view.getLayoutDirection(); (i'e : Class doesn;t work on all gingerbread devices )

so what i did was

replaced all

view.getLayoutDirection();

with the below code

GravityCompat.getAbsoluteGravity(gravity,ViewCompat.getLayoutDirection(this));

I have my support library updated to the latest also have extended the fullDrawerlayout to the support navigational drawer. Now it works fine Gingerbread devices as well

Gunderson answered 22/10, 2013 at 14:42 Comment(0)
T
4

Try out this worked for me :

<include
    android:id="@+id/left_drawer"
    android:orientation="vertical"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    layout="@layout/drawer"/>

Set width of included layout android:layout_width="320dp". For devices with different screen size you can dynamically set the width of this included layout.

Terraterrace answered 28/11, 2013 at 13:51 Comment(0)
F
4

Another possible way to solve the issue without overriding too much:

public class FullScreenDrawerLayout extends DrawerLayout {

... //List of constructors calling
... //super(...);
... //init();

/** Make DrawerLayout to take the whole screen. */
protected void init() {
    try {

        Field field = getClass().getSuperclass().getDeclaredField("mMinDrawerMargin");
        field.setAccessible(true);
        field.set(this, Integer.valueOf(0));

    } catch (Exception e) {
        throw new IllegalStateException("android.support.v4.widget.DrawerLayout has changed and you have to fix this class.", e);
    }
}

}

If, at some point, support library is updated and mMinDrawerMargin is not there anymore you will get exception and fix problem before you publish your next update.

I didn't make measurements, but suppose there is not so many reflection to affect performance. Furthermore, it executes only per view creation.

PS it's strange why DrawerLayout is made so inflexible (I'm about private min margin) at this point...

Fortis answered 27/12, 2013 at 10:34 Comment(0)
K
3

You can use this. Inspired by this post, I've upgraded for the 5th edition. Because it was having problems with StatusBar in versions 5 and later.

you have to extend DrawerLayout and override some methods because MIN_DRAWER_MARGIN is private

public class FullDrawerLayout extends DrawerLayout {

    private static final int MIN_DRAWER_MARGIN = 0; // dp

    public FullDrawerLayout(Context context) {
        super(context);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalArgumentException(
                    "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }

        setMeasuredDimension(widthSize, heightSize);

        //for support Android 5+
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
            params.topMargin = getStatusBarHeight();
            setLayoutParams(params);
        }

        // Gravity value for each drawer we've seen. Only one of each permitted.
        int foundDrawers = 0;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE) {
                continue;
            }

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (isContentView(child)) {
                // Content views get measured at exactly the layout's size.
                final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
                        widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
                final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
                        heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
                child.measure(contentWidthSpec, contentHeightSpec);
            } else if (isDrawerView(child)) {
                final int childGravity =
                        getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
                if ((foundDrawers & childGravity) != 0) {
                    throw new IllegalStateException("Child drawer has absolute gravity " +
                            gravityToString(childGravity) + " but this already has a " +
                            "drawer view along that edge");
                }
                final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                        MIN_DRAWER_MARGIN + lp.leftMargin + lp.rightMargin,
                        lp.width);
                final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                        lp.topMargin + lp.bottomMargin,
                        lp.height);
                child.measure(drawerWidthSpec, drawerHeightSpec);
            } else {
                throw new IllegalStateException("Child " + child + " at index " + i +
                        " does not have a valid layout_gravity - must be Gravity.LEFT, " +
                        "Gravity.RIGHT or Gravity.NO_GRAVITY");
            }
        }
    }

    boolean isContentView(View child) {
        return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
    }

    boolean isDrawerView(View child) {
        final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
        final int absGravity = Gravity.getAbsoluteGravity(gravity,
                child.getLayoutDirection());
        return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
    }

    int getDrawerViewGravity(View drawerView) {
        final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
        return Gravity.getAbsoluteGravity(gravity, drawerView.getLayoutDirection());
    }

    static String gravityToString(int gravity) {
        if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
            return "LEFT";
        }
        if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
            return "RIGHT";
        }
        return Integer.toHexString(gravity);
    }


    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
Kazukokb answered 12/8, 2017 at 9:5 Comment(0)
S
1

you can by below code

 int width = getResources().getDisplayMetrics().widthPixels/2;
        DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) drawer_Linear_layout.getLayoutParams();
        params.width = width;
        drawer_Linear_layout.setLayoutParams(params);
Soubrette answered 24/12, 2014 at 10:21 Comment(0)
T
1
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/app_bar_dashboard"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_marginRight="32dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <include layout="@layout/view_navigation_menu" />

    </android.support.design.widget.NavigationView>

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

That's works perfectly for me. Hope help others.

Truckage answered 3/11, 2017 at 5:45 Comment(0)
L
0

Google recommends having a maxim width of 320 dip as per the UI guidelines here. Moreover, the width can be set by specified the layout_width of the left_drawer ListView.

Leclaire answered 22/1, 2014 at 8:18 Comment(0)
T
0
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".UserListActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:background="@drawable/common_gradient"
    android:layoutDirection="rtl"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2">

        <TextView
            android:id="@+id/userType_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="نوع المستخدم"
            android:textColor="#000000"
            android:textSize="20sp"
            tools:text="نوع المستخدم" />

        <TextView
            android:id="@+id/className_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/userType_textView"
            android:layout_centerHorizontal="true"
            android:text="إسم القسم"
            android:textColor="#000000"
            android:textSize="16sp"
            tools:text="إسم القسم" />

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_alignBottom="@+id/userType_textView"
            android:layout_marginLeft="15dp"
            android:layout_marginStart="15dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/desc"
            android:onClick="showMenuAction"
            android:scaleType="fitCenter"
            android:src="@drawable/menu" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"

        android:background="#FAFAFA">

        <SearchView
            android:id="@+id/user_searchView"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="#9CC3D7" />

        <ListView
            android:id="@+id/users_listView"
            android:layout_width="100dp"
            android:layout_height="100dp"

            android:layout_alignParentBottom="true"
            android:layout_below="@+id/user_searchView"
            android:layout_centerHorizontal="true"
            android:divider="#DFDEE1"
            android:dividerHeight="1dp" />
    </RelativeLayout>

</LinearLayout>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/navigationDrawerUser"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:layoutDirection="rtl">


    <ExpandableListView
        android:id="@+id/menu_listView_user"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#195269"
        android:choiceMode="singleChoice"
        android:divider="#2C637D"
        android:dividerHeight="1dp"
        android:groupIndicator="@null">

    </ExpandableListView>

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

Thermoscope answered 22/5, 2017 at 14:9 Comment(0)
B
0

Everyone thinks that full-width Sidebar Drawer layout creation is very complicated, but it's very simple if you are following this layout pattern, you don't need to set any minus value.

This is my MainActivity.xml:

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main Activity -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbarMain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/layout_profile_toolbar"/>

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/app_navigation" />

    </LinearLayout>
    <!-- Main Activity End -->

    <!-- Custom Navigation Drawer Start -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <include
            android:id="@+id/custom_nav"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/fragment_profile"/>

    </com.google.android.material.navigation.NavigationView>
    <!-- Custom Navigation Drawer End -->

</androidx.drawerlayout.widget.DrawerLayout>
Biaxial answered 3/6, 2021 at 14:22 Comment(0)
N
0

You can set width programmatically. Give screen full width to navigation view's width.

NavigationView navigationView = findViewById(R.id.nav_view);
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) navigationView.getLayoutParams();
params.width = Utils.screenWidth(this);
Naresh answered 9/9, 2022 at 13:29 Comment(0)
R
-1

You can also take a look at SlidingDrawer class. It's a deprecated class, but as the documentation says you can write your own implementation based on its source code.

Relique answered 13/6, 2013 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.