1

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?

VanessaF
  • 515
  • 11
  • 36
  • Have you tried getting the menuItem like this and setting the setChecked(false); `[MenuItem menuItem =binding.navView.getMenu().findItem(R.id.navigation_home); menuItem.setCheckable(false);]` – Malik Saifullah Sep 08 '22 at 19:33
  • by my initial debugging `[item.getItemId()]` - `[navView.getSelectedItemId()]` - `[menuItem.getItemId()]` seems to be returning different set of id's. so which is the actually pointing to the menu item seems to be unclear at the moment. – Malik Saifullah Sep 08 '22 at 19:37
  • @MalikSaifullah: Thanks for your comments. Actually I don't really understand your instruction. What exactly shall I do and why are you always using the square brackets? Further, generally when you set setCheckable(false), the bottom is completely deactivated, meaning that you can't press it anymore. This is not what I want. Normally `item.setChecked(false);` should do what I want but strantely it does not work here. Do you know why or what I can do in order to remove the highlight from one button. – VanessaF Sep 09 '22 at 15:35
  • My bad i must have not proof read it. apart from square brackets you could go on and try the this code. `MenuItem menuItem =binding.navView.getMenu().findItem(R.id.navigation_home); menuItem.setCheckable(false);` in your if (item.getItemId()== R.id.BottomNavigation_Back) { } – Malik Saifullah Sep 09 '22 at 15:39
  • @MalikSaifullah: Thanks for your comments. When using your suggested code I get 2 error messages: "Cannot resolve symbol 'navView'" and "Cannot resolve symbol 'navigation_home'" – VanessaF Sep 09 '22 at 15:44
  • that's because you have to replace that with your id's of bottomNavigation and BottomNavigation_Back – Malik Saifullah Sep 09 '22 at 15:46
  • `navView` replace with `bottomNavigation` `navigation_home` replace with `BottomNavigation_Back` – Malik Saifullah Sep 09 '22 at 15:46
  • @MalikSaifullah: Thanks for your answer. Actually it works as it should. It'll be cool, if you write your comments in an answer. Then I'll accept it and award the bounty to you (and it might be helpful for others). – VanessaF Sep 09 '22 at 16:19

1 Answers1

1

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);
Malik Saifullah
  • 522
  • 1
  • 6
  • 22
  • Thanks Malik for your answer. It helped. I upvoted, accepted it and awarded the bounty to you. I really appreciate it. Maybe one follow-up question. When I start the app, the back-button of the BottomNavigationBar is highlighted at the very beginning until I press some other botton. Is there a way to deselect it from the very beginning? – VanessaF Sep 10 '22 at 08:02
  • Updated my answer, Good Luck. – Malik Saifullah Sep 10 '22 at 15:04
  • Thanks Malik for the update. Just for the record. Regarding the default selection your suggested code `binding.bottomNavigation.setSelectedItemId(R.id.BottomNavigation_Language);` does not work, as the back-button is still selected at the beginning. You have to use `binding.bottomNavigation.getMenu().findItem(R.id.BottomNavigation_Language).setChecked(true);` during the very first creation of the fragment. But that is just a side note. I highly appreciate your answer and effort. Thanks a lot. – VanessaF Sep 11 '22 at 08:05
  • 1
    Got it i did tested it out before posting hmm i will test that out again to correct my mistake.Thank you for letting me know Good Luck. – Malik Saifullah Sep 11 '22 at 08:34