Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 824
Emmanuel Njorodongo Avatar asked Jun 15 '20 10:06

Emmanuel Njorodongo


People also ask

What is DrawerLayout?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .

What is Appbarconfiguration in Android Studio?

The Openable layout indicating that the Navigation button should be displayed as a drawer symbol when it is not being shown as an Up button. Returns. Openable. The Openable layout that should be toggled from the Navigation button.


2 Answers

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.

like image 77
Kathir Avatar answered Sep 21 '22 15:09

Kathir


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

like image 43
Adm123 Avatar answered Sep 19 '22 15:09

Adm123