37

I am trying to implement a Navigation drawer, but I keep getting this error. I saw the similar questions but did not work for me. I have the following layout activity_main2.xml:

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

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

   <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:background="#fff"
            android:dividerHeight="1dp" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

On my activity_main2.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    if (toolbar != null) {
        //toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }
    initDrawer();
}

private void initView() {
    leftDrawerList = (ListView) findViewById(R.id.left_drawer);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    navigationDrawerAdapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
    leftDrawerList.setAdapter(navigationDrawerAdapter);
    leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 1){
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        }
    });
}

private void initDrawer() {
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ismail Rubad
  • 1,458
  • 2
  • 13
  • 27
  • Where do you get this error? On runtime or in Android Studio? Are you using the latest support library? – Jacob Ras Jul 31 '15 at 12:28
  • 1
    using Android Studio and got the runtime error: java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY. – Ismail Rubad Jul 31 '15 at 12:30

11 Answers11

50

My DrawerLayout was inside a LinearLayout:

<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="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/abohawawallpapersqr"
    tools:context=".MainActivity">

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

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

        <!-- ...... -->
        </android.support.v4.widget.DrawerLayout>
</LinearLayout>

Then I solved the problem by changing the layout_height="wrap_content" to layout_height="match_parent" of LinearLayout.So the code is:

<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"
    android:background="@drawable/abohawawallpapersqr"
    tools:context=".MainActivity">
Mike
  • 4,550
  • 4
  • 33
  • 47
Ismail Rubad
  • 1,458
  • 2
  • 13
  • 27
24

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

Edit

Make sure you are using right constraints to ensure that height is not 0dp when using constraint layout

VipulKumar
  • 2,385
  • 1
  • 22
  • 28
  • What'd you prefer instead? I replace it with LinearLayout and I get: Error inflating class android.support.constraint.LinearLayout – waseefakhtar Nov 23 '17 at 14:05
  • 2
    You're absolutely right. RelativeLayout worked best for me here. – waseefakhtar Dec 01 '17 at 02:09
  • 11
    @VipulKumar You should set both width and height of DrawerLayout to 0dp in ConstraintLayout. If only set height to 0dp, width to match_parent, which will cause this problem. https://stackoverflow.com/questions/47001482/drawerlayout-inside-constaintlayout-java-lang-illegalargumentexception-drawer – user175257 Jan 03 '19 at 08:30
  • 2
    @user175257 just tried out that solution and it worked for me. Would recommend trying this to those having issues with DrawerLayout combined with ConstraintLayout – caitcoo0odes May 10 '19 at 22:26
  • Please see other solutions about how to use DrawerLayout in ConstraintLayout – VipulKumar Jan 08 '21 at 16:30
23

For those who still have an exception, ensure your DrawerLayout's height is match_parent

YTerle
  • 2,606
  • 6
  • 24
  • 40
15

Make sure your activity is not a "Dialog" activity, check in the manifest. I made this mistake and got the error.

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
  • 3
    You saved my butt. I was convinced this was related to ConstraintLayout, but it was a good old Dialog themed Activity. Thanks! – Lisa Wray May 04 '18 at 02:26
9

This can also happen if you have set layout_width to wrap_content. In my case it was solved by setting width to match_parent. Hope this helps someone.

Dhrumil Patel
  • 544
  • 6
  • 16
3

use this ...

public class CustomDrawerLayout extends DrawerLayout {
public CustomDrawerLayout(Context context) {
       super(context);
}
public CustomDrawerLayout(Context context, AttributeSet attrs) {
       super(context, attrs);
 }
     public CustomDrawerLayout(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);
    }
}
1
requestWindowFeature(Window.FEATURE_NO_TITLE);

was solved my problem by creating the activity like below;

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}
1

If you still get this Exception make sure that your activity is full Screen defined In Manifest u can do in Activity:

android:theme="@style/Theme.AppCompat.Light.FullScreen"

Or define this in styles.xml:

 <style name="Theme.AppCompat.Light.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Manoj Rawat
  • 260
  • 4
  • 6
  • This was it for me, while trying to convert my old application to be more Dark theme friendly, [as per Android instructions](https://developer.android.com/guide/topics/ui/look-and-feel/darktheme). In my case my style was ` – Mendhak Sep 26 '21 at 16:57
1

Even I tried the Drawer's layout_height = "match_parent" but also java threw an error, so I just restarted android studio it worked no errors were found

SagaRock101
  • 129
  • 1
  • 7
0

You could try to set Exactly measurements explicitly always like below:

public class MyDrawerLayout extends DrawerLayout {

    public MyDrawerLayout(@NonNull Context context) {
        super(context);
    }

    public MyDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDrawerLayout(@NonNull Context context, @Nullable 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);
    }
}
sandrstar
  • 12,503
  • 8
  • 58
  • 65
0

By mistake, I set a custom dialog theme to the activity instead of a custom activity theme:

activity.setTheme(R.style.StyledDialogTheme);

After i corrected this, it workes again.

egmontr
  • 249
  • 3
  • 15