Skip to content Skip to sidebar Skip to footer

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 b

Solution 1:

Not sure about the proper way to achieve this but a work around will help-

  1. setCheckable(false) for first item

    navigation.getMenu().getItem(0).setCheckable(false);

  2. item.setCheckable(true) inside onNavigationItemSelected()

    publicbooleanonNavigationItemSelected(MenuItem item) {
    
    switch (item.getItemId()) {
        case R.id.navigation_home:
            item.setCheckable(true);
            mTextMessage.setText(R.string.title_home);
            returntrue;
      }
      returnfalse;
    }
    

Solution 2:

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"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/navigation_home"android:icon="@drawable/ic_home_black_24dp"android:title="@string/home" /><itemandroid:id="@+id/navigation_cart"android:icon="@drawable/ic_shopping_cart_black_24dp"android:title="@string/cart" /><itemandroid:id="@+id/navigation_wishlist"android:icon="@drawable/ic_favorite_border_black_24dp"android:title="@string/wish_list" /><itemandroid:id="@+id/navigation_account"android:icon="@drawable/ic_person_black_24dp"android:title="@string/account" /><!-- Our invisible item --><itemandroid: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

Solution 3:

XML Code

<groupandroid:checkableBehavior="single"><itemandroid:id="@+id/nav_item1" /><itemandroid:id="@+id/nav_item2" /><itemandroid:id="@+id/nav_item3" /><itemandroid:id="@+id/nav_item4" /><itemandroid: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(newBottomNavigationView.OnNavigationItemSelectedListener() {
            @OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem menuItem) {

            switch (menuItem.getItemId()) {
                case R.id.nav_item1:
                    returntrue;

                case R.id.nav_item2:
                    returntrue;

                case R.id.nav_item3:
                    returntrue;

                case R.id.nav_item4:
                    returntrue;
            }

            // Default operation you want to performreturnfalse;
        }
    });

Solution 4:

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 listenerreturn@OnNavigationItemSelectedListenertrue
            }
            R.id.option2 ->{
               item.isCheckable=true//notify the listenerreturn@OnNavigationItemSelectedListenertrue
            }
            R.id.option3 ->{
                //go to forgot user fragment
                item.isCheckable=true//notify the listenerreturn@OnNavigationItemSelectedListenertrue
            }

            else -> false
        }


    }

Finally , make a selector color so you can change easily in color

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_checked="true"android:color="@color/colorAccent" /><itemandroid: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.

Solution 5:

Add this line in your onCreate method

mBottomNavigation.setSelectedItemId("ID OF YOUR MENU ITEM");

Post a Comment for "Set No Item Pre-selected In Bottom Navigation View"