Boardy
Boardy

Reputation: 36217

DrawerLayout with AppCompat library Illegal Argument Exception

I am having a strange issue with the Drawer Layout and the new AppCompat in trying to make my app material designed.

I've done this with a similar activity with no problem but for this one I keep getting the following exception:

java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.

Below is my XML layout

<?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="fill_parent"
    android:layout_height="wrap_content">
    <include layout="@layout/toolbar"/>
    <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">
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <fragment android:name="com.BoardiesITSolutions.MysqlManager.QueryEditor"
                android:id="@+id/fragment_queryEditor"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                tools:layout="@layout/query_editor" />
            <fragment android:name="com.BoardiesITSolutions.MysqlManager.ResultViewer"
                android:id="@+id/fragment_resultViewer"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                tools:layout="@layout/query_editor" />
            <TextView android:id="@+id/query_lblStatus"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="5dp"
                android:textStyle="bold"/>
        </LinearLayout>
            <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/left_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:paddingLeft="@dimen/list_padding"
            android:paddingRight="@dimen/list_padding"
            android:choiceMode="singleChoice"
            android:divider="#4e4e4e"
            android:dividerHeight="1dp"
            android:background="#111" />
        <LinearLayout 
                android:layout_width="280dp"
                android:layout_height="match_parent"
                android:layout_gravity="end"
                android:paddingLeft="@dimen/list_padding"
                android:paddingRight="@dimen/list_padding"
                android:background="#4b4b4b">
                <TextView android:id="@+id/lblDBDetails_NotConnected"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center_horizontal"
                    android:textStyle="bold"
                    android:text="@string/not_connected_to_a_database_use_left_hand_menu_to_connect"
                    android:textColor="@color/black"/>
            <ExpandableListView
                android:id="@+id/right_drawer"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice"
                android:divider="#4e4e4e"
                android:dividerHeight="1dp"
                android:background="#4b4b4b" />
</LinearLayout>
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

Below is how I set up and initialise my Navigation Drawer

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    AppCompatNavigationManager navManager = new AppCompatNavigationManager(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_closed);
            mDrawerToggle = navManager.setDrawerToggle(toolbar);
            mDrawerLayout.setDrawerListener(mDrawerToggle);
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            navManager.prepareActionBar();

navManager.setDrawerToggle, returns the AppCompat drawer toggle with the following code

public ActionBarDrawerToggle setDrawerToggle(Toolbar toolbar)
    {   
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(
                activity, 
                drawerLayout, 
                toolbar,
                drawerOpen, 
                drawerClosed)
        {
            public void onDrawerClosed(View view)
            {
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView)
            {
                super.onDrawerOpened(drawerView);
            }
        };

        return actionBarDrawerToggle;
    }

navManager.prepareActionBar(); does the following code:

public void prepareActionBar()
    {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
        {
            activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            activity.getSupportActionBar().setHomeButtonEnabled(true);
        }
    }

My class extends the ActionBarActivity.

I don't understand what is making this activity so different that is causing it to throw the exception.

Upvotes: 0

Views: 613

Answers (1)

alanv
alanv

Reputation: 24124

You need to set android:orientation="vertical" and both android:layout_width and layout_height to "match_parent" in your root LinearLayout. You can technically leave "fill_parent" but this value is obsolete and maps to "match_parent".

The reason for this is that "wrap_content" will cause the parent LinearLayout to measure its child views who are using "match_parent" with measurement mode AT_MOST. This allows it to find the minimum size that will fit the child views.

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

Upvotes: 2

Related Questions