Set no item pre-selected in Bottom Navigation view
If anyone interested in a nice Kotlin solution, here's mine:
//disable the preselected first item
//<navigation> is the bottom navigation view
navigation.menu.getItem(0).isCheckable=false
Then in the selection listener, make sure that you'll show the user what he selected
BottomNavigationView.OnNavigationItemSelectedListener { item: MenuItem ->
when (item.itemId) {
R.id.option1 -> {
item.isCheckable=true //here is the magic
//notify the listener
return@OnNavigationItemSelectedListener true
}
R.id.option2 ->{
item.isCheckable=true
//notify the listener
return@OnNavigationItemSelectedListener true
}
R.id.option3 ->{
//go to forgot user fragment
item.isCheckable=true
//notify the listener
return@OnNavigationItemSelectedListener true
}
else -> false
}
}
Finally , make a selector color so you can change easily in color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="@color/colorAccent" />
<item android:color="@color/colorPrimary" />
And add the selector to the navigation view
app:itemIconTint="@color/navigation_colors"
app:itemTextColor="@color/navigation_colors"
Now, if you need to change the colours, just change the selector.
Not sure about the proper way to achieve this but a work around will help-
setCheckable(false)
for first itemnavigation.getMenu().getItem(0).setCheckable(false);
item.setCheckable(true) inside onNavigationItemSelected()
public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: item.setCheckable(true); mTextMessage.setText(R.string.title_home); return true; } return false; }
XML Code
<group android:checkableBehavior="single">
<item android:id="@+id/nav_item1" />
<item android:id="@+id/nav_item2" />
<item android:id="@+id/nav_item3" />
<item android:id="@+id/nav_item4" />
<item
android:id="@+id/nav_item5"
android:icon="@drawable/icon_item5"
android:title="Home"
android:visible="false"/>
</group>
JAVA Code
bottomNavigationView.getMenu().getItem(4).setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_item1:
return true;
case R.id.nav_item2:
return true;
case R.id.nav_item3:
return true;
case R.id.nav_item4:
return true;
}
// Default operation you want to perform
return false;
}
});
I came up with another solution
Just add one more item to your menu.xml file for example
This is my bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/home" />
<item
android:id="@+id/navigation_cart"
android:icon="@drawable/ic_shopping_cart_black_24dp"
android:title="@string/cart" />
<item
android:id="@+id/navigation_wishlist"
android:icon="@drawable/ic_favorite_border_black_24dp"
android:title="@string/wish_list" />
<item
android:id="@+id/navigation_account"
android:icon="@drawable/ic_person_black_24dp"
android:title="@string/account" />
<!-- Our invisible item -->
<item
android:id="@+id/invisible"
android:visible="false"
android:icon="@drawable/ic_person_black_24dp"
android:title="@string/account" />
</menu>
Notice that I have added that item at last position and given it an id invisible
and also set it's visibility to false.
Now, In the activity just set selected item id to this id like this
bottomNavMenu.setSelectedItemId(R.id.invisible);
Thanks