13

I am trying set up Navigation Drawer Layout with App Bar Configuration using the new Android Architecture. The problem am having is that android studio is telling me the way am setting up the drawer layout is deprecated.

Here is my xml configuration


<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/top_app_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/MaterialTheme"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            />

    </com.google.android.material.appbar.AppBarLayout>

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

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation_graph"/>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="230dp"
            android:layout_height="match_parent"
            android:background="@drawable/gradient_background"
            app:itemTextColor="@android:color/black"
            app:menu="@menu/drawer_menu"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:theme="@style/Theme.Design">

        </com.google.android.material.navigation.NavigationView>

    </androidx.drawerlayout.widget.DrawerLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Here is how i declare the AppBarConfiguration and Drawer Layout


    private AppBarConfiguration appBarConfiguration;
    private NavController navController;
    MaterialToolbar topAppBar;

    private ActionBarDrawerToggle actionBarDrawerToggle;
    DrawerLayout drawerLayout;
    NavigationView navigationView;

Here is where i find the NavController, topAppBar, drawerLayout and NavigationView

        navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        topAppBar = findViewById(R.id.top_app_bar);
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.navigation_view);

Here is the way am setting the drawer layout which is deprecated is there a new way of setting up the drawer layout that is not deprecated


    appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
                        .setDrawerLayout(drawerLayout)
                        .build();

Here is where am setting up with the NavigatioUi

NavigationUI.setupWithNavController(topAppBar, navController, appBarConfiguration);

Emmanuel Njorodongo
  • 1,004
  • 17
  • 35

2 Answers2

28

use setOpenableLayout(@Nullable Openable openableLayout) Your drawerLayout is implements Openable.

Adm123
  • 396
  • 3
  • 6
16

It's ironic that the latest Android Studio (v 4.0.1) itself uses setDrawerLayout(drawer) in its inbuilt NavigationDrawerActivity example.

The fix is very simple. Here's probably what you have:

mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)           // REPLACE THIS LINE
                .build();

Replace the above mentioned line with the following:

mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)        // WITH THIS LINE
                .build();

Source: Android Official Reference

The DrawerLayout class implements Openable. So this will simply work without requiring any other change from your end.

Kathir
  • 1,282
  • 1
  • 15
  • 19