I have implemented BottomNavigationView and have no idea how to set selection index or MenuItem
id (in my case, middle item should be selected by default).
I'm afraid there's no such possibility for now as far as it's too raw yet, but anyways any help will be appreciated. Thanks!
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.
The only solution that worked for me is:
View view = 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 user5968678 mentioned, a new method was added since Android Support Library v25.3.0:
bottomNavigationView.setSelectedItemId(R.id.item_id);
so use this instead :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With