Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidX Navigation Drawer

After searching and searching I can't make the full step to AndroidX. I have problems with Navigation Drawer.

<android.support.design.internal.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main_page"
    app:menu="@menu/activity_main_page_drawer" />

I have changed navigationView to bottomNavigationView.

    //NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    BottomNavigationView bottomNavView = (BottomNavigationView) findViewById(R.id.nav_view);
    bottomNavView.setNavigationItemSelectedListener(this);
    View headerView = bottomNavView.getHeaderView(0);
    TextView navUsername = (TextView) headerView.findViewById(R.id.textnav);
    navUsername.setText(str);


public class MainPageActivity extends AppCompatActivity
    implements BottomNavigationView.OnNavigationItemSelectedListener {

But I don't get it to work. Now other errors appear,

Error:

cannot find symbol method setNavigationItemSelectedListener(MainPageActivity) bottomNavView.setNavigationItemSelectedListener(this); ^ symbol: method setNavigationItemSelectedListener(MainPageActivity) cannot find symbol method getHeaderView(int) View headerView = bottomNavView.getHeaderView(0); ^ symbol: method getHeaderView(int)

How can I solve all these errors?

like image 413
diaconu liviu Avatar asked Jan 22 '26 20:01

diaconu liviu


1 Answers

You should use headerlayout for navigation drawer instead of BottomNavigation

AndroidX is a major improvement to the original Android Support Library. So to make project use androidX.

  • Goto Refactor-> Migrate to androidX.

  • Add implementation com.google.android.material.material:1.1.0-alpha09 at buid.gradle

In your xml:

<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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/menu_home_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

In your java file:

 navigationView.setNavigationItemSelectedListener(YourClass.this);

Inside app level theme:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
like image 82
Sumit Shukla Avatar answered Jan 25 '26 09:01

Sumit Shukla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!