Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom NavigationView change items programmatically

Tags:

android

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"/>
like image 205
Imtnan Akram Avatar asked Nov 29 '16 13:11

Imtnan Akram


3 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);
like image 96
Michael42 Avatar answered Nov 01 '22 12:11

Michael42


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

like image 28
Imtnan Akram Avatar answered Nov 01 '22 12:11

Imtnan Akram


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);    
like image 2
aymhenry Avatar answered Nov 01 '22 13:11

aymhenry