androidnavigation-drawerslidingdrawer

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


I'm working on an app but I have some troubles with drawer layout. I always getting the error message like I mentioned in the title but I have no such Gravity with LEFT value. All I have are gravities with END values and that's all. Here is my Java code:

public class HomeActivity extends AppCompatActivity {

private ActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Toolbar mainToolBar =findViewById(R.id.main_page_toolbar);
    DrawerLayout mainDrawerLayout = findViewById(R.id.main_page_drawer_layout);
    NavigationView mainNavigationView = findViewById(R.id.main_page_navigation_view);

    setSupportActionBar(mainToolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);


    toggle = new ActionBarDrawerToggle(this,mainDrawerLayout,mainToolBar,R.string.openDrawer,R.string.closeDrawer);
    mainDrawerLayout.addDrawerListener(toggle);
    toggle.syncState();

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    toggle.onConfigurationChanged(newConfig);
}
}

And here is my XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
 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:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:openDrawer="end"
 tools:context=".Activities.HomeActivity"
 android:id="@+id/main_page_drawer_layout"
 android:fitsSystemWindows="true">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/home_main_page"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    app:headerLayout="@layout/home_side_menu_header_layout"
    app:menu="@menu/home_side_menu"
    android:layoutDirection="rtl"
    android:id="@+id/main_page_navigation_view"/>


Solution

  • 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)