I'm adding the new Bottom Navigation View from the material design library to a project, and I would like to have no pre selected item by default.
For now first item is selected by default.
I have used
mBottomNavigation.getMenu().getItem(0).setChecked(false);
but when doing it in for loop for all the menu item last item is selected again by default.
Is there a way we can achieve this?
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
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