Search code examples
androidbottomnavigationview

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?


Solution

  • I combined the solution mentioned by @Ashish Kumar and resolved my query and

    private void customizeBottomBar() {
            mBottomNavigation.getMenu().getItem(0)
                    .setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_reserve_normal));
            changeMenuItemCheckedStateColor(mBottomNavigation, getUnCheckedColor(), getUnCheckedColor());
        }
    
    /**
       * Method to change the color state of bottom bar view
       * @param bottomNavigationView - BottomNavigation view instance
       * @param checkedColorHex int value of checked color code
       * @param uncheckedColorHex int value of unchecked color code
       */
      void changeMenuItemCheckedStateColor(BottomNavigationView bottomNavigationView,
          int checkedColorHex, int uncheckedColorHex) {
    
        int[][] states = new int[][]{
            new int[]{-android.R.attr.state_checked}, // unchecked
            new int[]{android.R.attr.state_checked}, // checked
        };
    
        int[] colors = new int[]{
            uncheckedColorHex,
            checkedColorHex
        };
    
        ColorStateList colorStateList = new ColorStateList(states, colors);
        bottomNavigationView.setItemTextColor(colorStateList);
        bottomNavigationView.setItemIconTintList(colorStateList);
    
      }