I am trying to set default item on activity created but it isn\'t working? This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.o
FYI: for fragment, onCreateView
BottomNavigationView mBottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView);
mBottomNavigationView.setSelectedItemId(R.id.your_item);
Kotlin extension version of Abshishek's answer:
internal fun BottomNavigationView.checkItem(actionId: Int) {
menu.findItem(actionId)?.isChecked = true
}
// use
bottom_navigation.checkItem(R.id.navigation_home)
This does not trigger OnNavigationItemSelectedListener
.
You can use:
navigationView?.menu?.findItem(drawableMenuItem.id)?.isChecked = true
and it will not fire OnNavigationItemSelectedListener
events.
You can also set selection in BottomNavigatioView using index like this :
public void selectBottomNavigationOption(int index) {
switch (index) {
case 0:
index = R.id.action_1;
break;
case 1:
index = R.id.action_2;
break;
case 2:
index = R.id.action_3;
break;
case 3:
index = R.id.action_4;
break;
}
bottomNavigationView.setSelectedItemId(index);
}
Instead of selected you need to setChecked(true)
that item. Try this code
mBottomNavigationView=(BottomNavigationView)findViewById(R.id.bottom_nav);
mBottomNavigationView.getMenu().findItem(R.id.item_id).setChecked(true);
Checked item is highlighted in BottomNavigationView
.
In a Fragment
getActivity()?.myNavigationViewId?.selectedItemId = R.id.other_tab_id
In an Activity
myNavigationViewId?.selectedItemId = R.id.other_tab_id
NOTE: Make sure to replace
myNavigationViewId
andother_tab_id
with your actual navigation view and tab ids.