Home » Android » android – Bottom NavigationView change items programmatically

android – Bottom NavigationView change items programmatically

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using Bottom NavigationView and want to change items programmatically. There are 5 items and when I set 4th item selected, First item still remains active.

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/color_white"
        app:itemIconTint="@color/drawer_item"
        app:itemTextColor="@color/drawer_item"
        android:layout_gravity="start"
        app:menu="@menu/nav_bar_menu"/>
How to&Answers:

From API 25.3.0,
you can use setSelectedItemId

BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.item_4th);

Answer:

There is no possible way to change items through code currently but found the solution here
Set initially selected item index/id in BottomNavigationView

Answer:

This worked for me
Saved me playing with fragments

NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.getMenu().findItem(R.id.my_navigation_item).setChecked(true);
navigationView.getMenu().performIdentifierAction(R.id.my_navigation_item, 0);