android-studiouinavigationcontrollernavigation-drawerappbar

setDrawerLayout(androidx.drawerlayout.widget.DrawerLayout) is deprecated


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


Solution

  • use the setOpenableLayout(@Nullable Openable openableLayout) method of AppBarConfiguration.Builder

    DrawerLayout implements Openable.

    Example

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