10

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"/>
Imtnan Akram
  • 123
  • 1
  • 1
  • 6
  • You may check my answer here http://stackoverflow.com/questions/40202294/set-selected-item-in-android-bottomnavigationview/40721016#40721016, I believe it is very similar to your problem. – Viacheslav Nov 29 '16 at 13:22
  • Thanks @Viacheslav found the hack here http://stackoverflow.com/questions/40236786/set-initially-selected-item-index-id-in-bottomnavigationview – Imtnan Akram Nov 29 '16 at 13:30

4 Answers4

16

From API 25.3.0, you can use setSelectedItemId

BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.item_4th);
ANUJ GUPTA
  • 905
  • 13
  • 22
Michael42
  • 881
  • 1
  • 8
  • 15
  • If you need to dynamically pass different Fragment arguments to the loaded Fragment take a look at this solution: https://stackoverflow.com/a/57526788/4034572 – Albert Vila Calvo Aug 16 '19 at 15:03
2

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

Community
  • 1
  • 1
Imtnan Akram
  • 123
  • 1
  • 1
  • 6
2

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);    
aymhenry
  • 81
  • 4
0

I used this method on Android 13.0. This simply changes an active item without clicking that item.

_nav.getMenu().findItem(R.id.my_first).setChecked(true);
dotrinh PM
  • 903
  • 1
  • 7
  • 19