Set initially selected item index/id in BottomNavigationView

43,194

Solution 1

Set the selected menu item ID using setSelectedItemId:

bottomNavigationView.setSelectedItemId(R.id.item_id);

This method started being available from Android Support Library 25.3.0.

Solution 2

The only solution that worked for me is:

View view = bottomNavigationView.findViewById(R.id.menu_action_dashboard);
view.performClick();

Simply performing click does the trick. Hope we'll get extra methods/properties in future releases.

UPD:

As user5968678 mentioned, a new method was added since Android Support Library v25.3.0:

bottomNavigationView.setSelectedItemId(R.id.item_id);

so use this instead :)

Solution 3

I think this solution my be slightly more elegant than accepted answer:

bottomNavigationView.getMenu().getItem(menuItemIndex).setChecked(true)

where menuItemIndex is index of the selected element.

Solution 4

Here's what the documentation says about it:

Menu items can also be used for programmatically selecting which destination is currently active. It can be done using MenuItem#setChecked(true)

As an alternative to what Jan posted, you can also find the item by id:

Menu menu = findViewById(R.id.navigation).getMenu();
menu.findItem(R.id.navigation_home).setChecked(true);

Also, in general, I can recommend calling .callOnClick() instead of .performClick().

Solution 5

I believe the question in this context is being viewed in different contexts basing on answers here. According to assessment, whats required is ability to focus on specific BottomNavigationView item (definitely in new class holding different fragments).

Now, you could have BottomNavigationView OR Buttons or Anything clickable to launch new activity on intent : - i.e

Intent intent = new Intent(getActivity(), New_Activity.class);
    intent.putExtra("EXTRA_PAGE, 1);
    startActivityForResult(intent, 30);

Then -in our New_Activity, we receive the intent-

Intent intent = getIntent(); int page = intent.getExtras().getInt("EXTRA_PAGE);

We then loop over the page variable to find the number/Index for which the current BottomNavigationView is reflecting , THEN we set our focus menu item (assuming your BottomNavigationView has Menu Item for its display)

     if(page == 1) {
         currentselect = new Pending();
            bottomNavigationView.getMenu().getItem(0).setChecked(true);
}

This answers the question above. The rest of Fragment switch is handled well by number of posts above by invoking :

bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

Then something like :

   private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFrag = null;
                switch (item.getItemId()) {
                    case R.id.pending:
                        selectedFrag = new Pending();
                        break;
                    case R.id.onTransmit:
                        selectedFrag = new inTransmit();
                        break;
                    case R.id.complete:
                        selectedFrag = new Complete();
                        break;
                }

                getSupportFragmentManager().beginTransaction().replace(R.id.select_field, selectedFrag).commit();

                return true;
            }
        };

NOTE: Using BottomNavigationView and ContentFrameLayout is soo economical and will slash down your code to over 50 % unlike using likes of ViewPager and Tablayout

Share:
43,194
UneXp
Author by

UneXp

Updated on August 30, 2021

Comments

  • UneXp
    UneXp over 2 years

    I have implemented BottomNavigationView and have no idea how to set selection index or MenuItem id (in my case, middle item should be selected by default).

    I'm afraid there's no such possibility for now as far as it's too raw yet, but anyways any help will be appreciated. Thanks!

  • Filipe Brito
    Filipe Brito over 7 years
    Reflection is a bad idea!
  • Meanman
    Meanman over 7 years
    Best solution yet
  • Anne-Claire
    Anne-Claire over 7 years
    Does not work. If you have animation enabled on your bottomNavigationBar you can see that color have been changed on the item that is selected, but animation is still on the first item of menu. Not available for now in the android lib. IF YOU WANT TO MAKE IT WORK ANYWAY: ((BottomNavigationMenuView) bottomNavigationView.getChildAt(0)).getChildAt(menuItemIndex‌​).performClick(); OR EVEN BETTER: ((BottomNavigationMenuView) bottomNavigationView.getChildAt(0)).findViewById(R.id.id_men‌​u_action).performCli‌​ck();
  • Jan Slominski
    Jan Slominski over 7 years
    Can you post your code? I'm using latest BottomNavigationView from google support library (25.1.0) and when I change selected index like that the animation works without problems.
  • Irfandi D. Vendy
    Irfandi D. Vendy almost 5 years
    this will only set the menu to active, but not to trigger the menu itself, which is what i'm looking for