Android Drawerlayout - No Drawer View Found with Gravity

Android DrawerLayout - No drawer view found with gravity

From documentation

To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>

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

</LinearLayout>

<fragment
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="@layout/fragment_navigation" />

</android.support.v4.widget.DrawerLayout>

No drawer view found with gravity LEFT

Content and Drawer must be two children of the DrawerLayout. So change your layout like that:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />

<android.support.v7.widget.RecyclerView
android:id="@+id/book_list_rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />

</LinearLayout>

<ListView
android:id="@+id/ListView1"
android:layout_width="241dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#333"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15sp"
android:paddingRight="15sp" />

</android.support.v4.widget.DrawerLayout>

Toolbar - No drawer view found with gravity LEFT

you can not put DrawerFragment inside child view of DrawerLayout You must have to provide drawer layout or drawer fragment as child view of Drawer layout. To solve your issue Simply put Fragment outside of AppbarLayout.
just like that :

        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>

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

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp">

<include android:id="@+id/app_bar"
layout="@layout/toolbar" />

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="48dp"/>

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_below="@id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</android.support.design.widget.AppBarLayout>

<fragment
android:name="com.bestworkouts.sheikoworkout.NavigationDrawerFragment"
android:id="@+id/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

Always getting the error messsage: No drawer view found with gravity LEFT

You need to handle navigation click on Toolbar like below:

mainToolBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mainDrawerLayout.isDrawerOpen(GravityCompat.END))
mainDrawerLayout.closeDrawer(GravityCompat.END);
else
mainDrawerLayout.openDrawer(GravityCompat.END);
}
});

Also don't forgot to close drawer whenever needed like below:

mainDrawerLayout.closeDrawer(GravityCompat.END)


Related Topics



Leave a reply



Submit