How to make a full-screen DrawerLayout in Android?
Asked Answered
A

5

6

Essentially, I need an "options" menu that can be accessed by swiping the screen left to right (or clicking on the options button in the left top corner of the screen). I also need it to cover the screen, but not fully replace it (it needs to be semi-opaque so that the previous menu is visible beneath it). What I have so far (I'm working on someone else's code, still haven't dechiphered it all, sorry for the lack of information):

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

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/blue"
        />



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

I have some functions to fill in the layout with clickable options, however, I can't make the options go full screen. I swipe left to right, and it only goes about 75% of the way. How can I make it a full-screen options panel? (I can't make it a new Activity, it needs to overlap with the previous one)

I have the opacity handled and the options buttons, I just can't make this go all the way to the right side of the screen. :D

Apogee answered 9/2, 2015 at 19:13 Comment(1)
Did you find the solution to your question? Can I help you any more?Softy
S
8

This works on all android versions:

View mSlidingView = findViewById(R.id.slider);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mSlidingView.getLayoutParams();
params.width = metrics.widthPixels;
mSlidingView.setLayoutParams(params);

Put this In your onCreate.

Softy answered 16/4, 2016 at 6:46 Comment(0)
K
2
<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"/>
Kun answered 19/5, 2016 at 5:53 Comment(1)
The best solution to add: android:layout_marginLeft="-65dp"Kun
C
2

The best way to do this is to create a custom DrawerLayout. I found this solution to work perfectly. Link to custom Full-Screen drawer layout implementation

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);
}
}
Circosta answered 31/5, 2017 at 16:16 Comment(0)
A
0

It's too late to answer, but for future reads of this question, I am giving my 2c tip: view which holds drawer should have end and right margins set to -64dp, because this value is aparently fixed in the code for rendering drawer.

So, in this case and since android:layout_gravity="start", ListView should have two additional parameters set as follows:

<ListView
...
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp" />

On the other hand, if android:layout_gravity="end", then android:layout_marginStart and android:layout_marginLeft should be set to -65dp.

Andreas answered 17/12, 2015 at 21:42 Comment(4)
Works for me. Still.Himalayas
Did you try on Marshmallo or N?Softy
Tested right now on Android 6.0 on Sony Xperia Z Compact. Didn't test on N, though. I'll try today to post pastebin snippet with layout and code.Himalayas
I have tested it on LG G4, Android M and it didn't work :(. According to this answer: https://mcmap.net/q/377839/-full-width-navigation-drawer I think its not working any more on newer devicesSofty
B
0
    FrameLayout frameLayout = findViewById(R.id.frame_menu);
            DrawerLayout.LayoutParams fparams = (DrawerLayout.LayoutParams) frameLayout.getLayoutParams();

DisplayMetrics displaymetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay()
                    .getMetrics(displaymetrics);
            int w = displaymetrics.widthPixels;

            fparams.width = w;
            frameLayout.setLayoutParams(fparams);
            frameLayout.requestLayout();
Boger answered 30/1, 2019 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.