Set Initially Selected Item Index/id In Bottomnavigationview
Solution 1:
Set the selected menu item ID using setSelectedItemId
:
bottomNavigationView.setSelectedItemId(R.id.item_id);
This method started being available from Android Support Library 25.3.0.
Solution 2:
The only solution that worked for me is:
Viewview= bottomNavigationView.findViewById(R.id.menu_action_dashboard);
view.performClick();
Simply performing click does the trick. Hope we'll get extra methods/properties in future releases.
UPD:
As user5968678mentioned, a new method was added since Android Support Library v25.3.0:
bottomNavigationView.setSelectedItemId(R.id.item_id);
so use this instead :)
Solution 3:
I think this solution my be slightly more elegant than accepted answer:
bottomNavigationView.getMenu().getItem(menuItemIndex).setChecked(true)
where menuItemIndex is index of the selected element.
Solution 4:
Here's what the documentation says about it:
Menu items can also be used for programmatically selecting which destination is currently active. It can be done using
MenuItem#setChecked(true)
As an alternative to what Jan posted, you can also find the item by id:
Menumenu= findViewById(R.id.navigation).getMenu();
menu.findItem(R.id.navigation_home).setChecked(true);
Also, in general, I can recommend calling .callOnClick()
instead of .performClick()
.
Solution 5:
If you're using listener, like default implementation in android studio, try this:
BottomNavigationViewnavigation= (BottomNavigationView) findViewById(R.id.navigation);
IntegerindexItem=4;
navigation.getMenu().getItem(indexItem).setChecked(true);
mOnNavigationItemSelectedListener.onNavigationItemSelected(navigation.getMenu().getItem(indexItem));
Post a Comment for "Set Initially Selected Item Index/id In Bottomnavigationview"