Set no item pre-selected in Bottom Navigation view

MRX picture MRX · Jun 12, 2017 · Viewed 14k times · Source

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?

Answer

Ashish Kumar picture Ashish Kumar · Jun 13, 2017

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()

    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;
    }