3

According to this related question Make drawerlayout inside fragment full screen height I am not sure if right or wrong but it seems like fragment is always not full screen, i.e it only duplicate the layout property of it content and use it, which is why you can have two or more fragments in one layout.

Right now I am having problem setting the hight of a Drawerlayout to match_parent, the full stack of the error I get is pasted below. I have tried Every means that I know to forceset the layoutparameter but it's just not working, and if I give the layout a specific dp like 500dp it will then work.

FATAL EXCEPTION: main
                                                                       Process: whalescorp.fabuloxity, PID: 16319
                                                                       java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
                                                                           at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1041)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                                                                           at android.view.View.measure(View.java:19857)
                                                                           at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
                                                                           at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                                                                           at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                                                                           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                           at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
halfer
  • 19,824
  • 17
  • 99
  • 186
Wale
  • 1,644
  • 15
  • 31

2 Answers2

4

This can also happen because of the new ConstraintLayout. Make sure the drawer layout isn't in the hierarchy of ConstraintLayout.

This is a really silly bug on Android's part. I will try to file a bug for it.

Timur Mukhortov
  • 598
  • 6
  • 12
3

I decide to create a custom DrawerLayout by extending DrawerLayout itself, and did nothing much but duplicated the methods in the DrawerLayout. But the only thing I did was to @Override the onMeasure method in the DrawerLayout.

Below is my Custom DrawerLayout and how I called it in my layout::

public class WhalesDrawerLayout extends DrawerLayout {

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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

And in your layout should be called like this:

<whalescorp.projectname.packagename.WhalesDrawerLayout
    android:id="@+id/drawer"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

</whalescorp.projectname.packagename.WhalesDrawerLayout>

By typing the class name in your activity.xml, it will bring the suggestion and you can easily get it that way also, meanwhile you will need to rebuild your project before it work, and you can call it in your code like this::

final WhalesDrawerLayout drawerLayout = l.findViewById(R.id.drawer); 
halfer
  • 19,824
  • 17
  • 99
  • 186
Wale
  • 1,644
  • 15
  • 31