Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set no item pre-selected in Bottom Navigation view

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?

like image 243
MRX Avatar asked Jun 12 '17 11:06

MRX


1 Answers

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

like image 166
AgentP Avatar answered Oct 01 '22 03:10

AgentP