Search code examples
javaandroidbottom-navigation-bar

How to remove highlight from one specific icon in a Botton Navigation Bar in Android


I have a Bottom Navigation Bar in Android with 4 icons. At the moment, when I click on an icon it gets highlighted (change of color) until another button is pressed on the Bottom Navigation Bar. While this is good for 3 of the 4 icons, I also have a "Back" button where this should not be the case. So if the Back button is pressed, it should either not be highlited at all, or it should just be highlighted for a very short period of time. Can you tell me how to do this in the Java code?

Here is the code that defines what happens if a button is pressed on the Botton Navigation Bar inside the onCreate method of the main acticity:

binding.bottomNavigation.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        if (item.getItemId()== R.id.BottomNavigation_Back) {

                            item.setChecked(false);
                            navController.navigateUp();

                        }

                        if (item.getItemId()== R.id.BottomNavigation_Language) {
                            navController.navigate(R.id.FR_LanguageSelection);
                        }

                        if (item.getItemId()== R.id.BottomNavigation_Info) {
                            navController.navigate(R.id.FR_Info);
                        }

                        if (item.getItemId()== R.id.BottomNavigation_Menu) {
                            navController.navigate(R.id.FR_Menu);
                        }


                        return true;
                    }
                });

The relevant chase is the first one if (item.getItemId()== R.id.BottomNavigation_Back) . I tried to use item.setChecked(false); but this does not work (as you can see in the screenshot). The Back Icon is still highlighted until another button is pressed. Any idea how to do this?

enter image description here

Reminder: Does anybody have an idea how to do this or why the command item.setChecked(false); does not work? Strangely when using item.setChecked(true); the behaviour is exactly the same. Is there a way how to disable the highlight from this specific button after is has been pressed?


Solution

  • You could access the menu item which is being selected like this and not set it checkable and rest should work as is.

    MenuItem menuItem =binding.navView.getMenu().findItem(R.id.navigation_home); 
    menuItem.setCheckable(false);
    

    You can choose which item should be selected by default by (Replace R.id.navigation_dashboard with your relevant id)

    binding.navView.setSelectedItemId(R.id.navigation_dashboard);
    

    This will only change the selection now to set the correct fragment according to the id do this.

    navController.navigate(R.id.navigation_dashboard);