30

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

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

Adding that to the layout xml as per Create a Navigation Drawer documentation produces

Exception raised during rendering: DrawerLayout must be measured with MeasureSpec.EXACTLY.
Exception details are logged in Window > Show View > Error Log

java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:591)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15518)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:607)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:509)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:440)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1545)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1302)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1059)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegatePageChange(LayoutEditorDelegate.java:686)
    at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.pageChange(CommonXmlEditor.java:360)
    at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:292)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1079)
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3028)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1749)
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:278)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3562)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3186)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Ahmad
  • 69,608
  • 17
  • 111
  • 137
gaara87
  • 2,087
  • 3
  • 21
  • 43

6 Answers6

38

I have similar problem. If you want preview layout for a moment, you can temporally change the width and height to absolute.

eg:-

<android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="400px"

    android:layout_height="800px">
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Asmok
  • 497
  • 3
  • 6
  • 2
    Addition: If there is a view that wraps the DrawerLayout then it should have `android:layout_height="match_parent"` but NOT wrap_parent otherwise this error will occur – Diolor Jan 19 '15 at 13:16
19

This is fixed in version 18 of the support library, released in July.

in Eclipse right click your project > select android tools > add support library.

Fixed. :)

Joakim
  • 446
  • 4
  • 8
17

From another Question I solved the problem by extending DrawerLayout, and forcing the correct Measure_Specs:

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);
    }

}
Community
  • 1
  • 1
joecks
  • 4,539
  • 37
  • 48
  • 1
    This is remains a problem at runtime problem, even though Google claims to have fixed this in Android Support Library revision 19 (October 2013). This hack works much better than my previous hack. – DataGraham Nov 14 '13 at 17:17
  • this should be on the top – Uzair Mughal Aug 24 '20 at 11:27
9

Asmok's answer is nice but I'm this type of person always forgetting to reverse such "temporary" edits. (In other words temporary may become very big ^^)

So instead I use the <include> tag and split the layout in two files.

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

    <!-- The main content view -->
    <include layout="@layout/main_layout" />

    <!-- The navigation drawer -->
    <include layout="@layout/navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>

For the main layout (which is the UI of the activit in question) I can now use Eclipse's graphical editor as all the content lives in a single file main_layout.xml.
Furthermore I can easily include the navigation drawer in different activities without the need to copy and paste the code.

nuala
  • 2,681
  • 4
  • 30
  • 50
  • 1
    @HiteshDhamshaniya: That's sad news to hear. :C Maybe write another question where you can show some of your XML and how you tried to implement it as it will be much clearer to read than discussing the issue via comments. You can use `share` link to reference to this answer and provide a link to your question in the comments here. So it will allow others to follow your paths more easily :) whatever happens: don't panic! – nuala Aug 26 '13 at 12:04
  • 1
    thanks for reply, but as temporary basis I should go with Asmok suggested way, it work – Hitesh Dhamshaniya Aug 27 '13 at 05:11
5

The easiest solution I found was to put in the preview Theme.Holo.Light.Dialog.FixedSize functional and peaceful ...

Image here

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Walterjp
  • 61
  • 1
  • 1
  • +1 from me for opening a StackOverflow account and supplying a solution, which although it's not optimal - it does work. People are too trigger-happy around here with the downvote guns... – AVIDeveloper Jan 20 '14 at 20:32
2

Custom Views are difficult to display correctly for the UI editor in eclipse (same goes for the UI editor in Android Studio). This is normal.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 2
    I wouldn't accept this as normal but it's true that UI tools aren't optimal for Android. It has improved a lot last years but it's sad even official components like DrawerLayout can't be rendered without errors. – Sander Versluys Jun 17 '13 at 14:23