Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set initially selected item index/id in BottomNavigationView

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!

like image 703
UneXp Avatar asked Oct 25 '16 09:10

UneXp


3 Answers

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.

like image 77
user5968678 Avatar answered Oct 12 '22 11:10

user5968678


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 :)

like image 57
UneXp Avatar answered Oct 12 '22 10:10

UneXp


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.

like image 21
Jan Slominski Avatar answered Oct 12 '22 11:10

Jan Slominski