I am trying to implement a Navigation drawer, but I keep getting the error DrawerLayout must be measured with MeasureSpec.EXACTLY excpetion
.
I saw the similar questions but did not work for me. I tried overriding the DrawerLayout's on measure method based on accepted answer in this link. I created a custom class (not an inner class in fragment class.) with fully qualified naming. But this approach does not work properly. See below image (drawer menu is always visible and other views are inactive):
I have the following layout fragment_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<com.example.win2.ebook.CustomDrawerLayout
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 -->
<!-- The main content view -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_frame"
android:orientation="vertical"
>
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/fragment_ebook_list"
/>
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</com.example.win2.ebook.CustomDrawerLayout>
and related part of my fragment is:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
CustomDrawerLayout mDrawerLayout;
mDrawerLayout = (CustomDrawerLayout) view.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) view.findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.drawer_list_item, R.id.drawer_text_title, mPlanetTitles));
I also examined specific sizes for height and width of drawer layout which results in unexpected margins.
How can I fix the problem? Does including another layout inside fragment_main.xml
have any (bad) effect? Thank You in Advance.
EDIT: I have an activity which is hosting two fragments. activity_twopane.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/dividerVertical"
android:showDividers="middle"
android:id="@+id/twopane">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_container"
android:layout_alignParentTop="true">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ad_fragment_container"
android:layout_alignParentBottom="true">
</FrameLayout>
</RelativeLayout>
In code, fragment_container
consists of a single recyclerview.