No drawer view found with gravity LEFT - Android
Asked Answered
G

5

1

My menu was opening from left to right, but I want it to be opened from right to left. I have arranged the code to be opened from right to left, but now the code does not work I get the following error.

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

I get an error on this line : return NavigationUI.navigateUp(navController, mAppBarConfiguration)

MAİN CLASS

public class MainActivity extends AppCompatActivity {
    private AppBarConfiguration mAppBarConfiguration;
    NavigationView navigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
     mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_sss, R.id.nav_gelisim,
                R.id.nav_destek,R.id.nav_hakkımızda,R.id.nav_instagram)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    }

XML

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


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layoutDirection="rtl"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:itemTextColor="@color/white"
        app:itemTextAppearance="@style/NavDrawerTextStyle"
        android:layout_gravity="right"
        android:theme="@style/NavigationView"
        android:background="@color/NavItem"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main_drawer"/>


</androidx.drawerlayout.widget.DrawerLayout>
Galah answered 12/1, 2020 at 17:18 Comment(0)
R
1

You need to handle navigation click on Toolbar like below:

ViewCompat.setLayoutDirection(toolbar, ViewCompat.LAYOUT_DIRECTION_RTL);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (drawer.isDrawerOpen(GravityCompat.END))
            drawer.closeDrawer(GravityCompat.END);
        else
            drawer.openDrawer(GravityCompat.END);
    }
});

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

drawer.closeDrawer(GravityCompat.END)
Respect answered 12/1, 2020 at 17:24 Comment(0)
U
0

You could possible find your answer here in this previous post:

Android DrawerLayout - No drawer view found with gravity

Urinary answered 12/1, 2020 at 17:21 Comment(0)
T
0

You have to set layout Direction in Your Root Drawer Layout

 android:layoutDirection="rtl"
Trappist answered 24/11, 2020 at 0:57 Comment(0)
T
0

If you set the layout direction of the root drawer as follows:

android:layoutDirection="rtl"

All elements will change direction. To solve this, place all elements within a LinearLayout and set LinearLayout direction to ltr.

    <androidx.drawerlayout.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        **android:layoutDirection="rtl"**
        >
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        **android:layoutDirection="ltr"**>
        <!--place your layout here-->
        </LinearLayout>
        <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layoutDirection="rtl"
        app:menu="@menu/drawer_menu" />
  </androidx.drawerlayout.widget.DrawerLayout>

If you are using the navigation component use the below code in your activity class.

override fun onCreate(savedInstanceState: Bundle?) {
    //...
    val navHostFragment = supportFragmentManager.findFragmentById(
            R.id.my_nav_host_fragment
        ) as NavHostFragment
        navController = navHostFragment.navController
    binding.navigationView.setupWithNavController(navController)
    //...
    }
  override fun onSupportNavigateUp(): Boolean {
            return NavigationUI.navigateUp(navController, binding.drawerLayout)
        }
  override fun onBackPressed() {
      if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
         binding.drawerLayout.closeDrawer(GravityCompat.START)
      } else {
          super.onBackPressed()
      }
  }
Transposal answered 1/6, 2021 at 10:57 Comment(0)
C
0

It seems like the DrawerLayout need to be in the start direction every time, if you want it to open from the left you need to do the following:

  • make your DrawerLayout android:layoutDirection="ltr"
  • remove tools:openDrawer from your DrawerLayout XML
  • put your NaviagtionView android:layout_gravity="start"
  • if you want your content to be RTL don't forget to do this in your contentView container android:layoutDirection="rtl"

and it will work like a charm!

Cotta answered 31/3, 2022 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.