166

I am using the new android.support.design.widget.BottomNavigationView from the support library. How can I set the current selection from code? I realized, that the selection is changed back to the first item, after rotating the screen. Of course it would also help, if someone could tell me, how to "save" the current state of the BottomNavigationView in the onPause function and how to restore it in onResume.

Thanks!

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Michael
  • 2,021
  • 3
  • 12
  • 18
  • 1
    I've just had a look at the source and I have the same question.. It doesn't look like there is a method to set the selected item, and `BottomNavigationView` doesn't do any internal saving of state. Probably expect this to be included in a future update. Duplicate (with some more info) here: http://stackoverflow.com/questions/40236786/set-initially-selected-item-index-id-in-bottomnavigationview – Tim Malseed Oct 26 '16 at 09:06

24 Answers24

228

From API 25.3.0 it was introduced the method setSelectedItemId(int id) which lets you mark an item as selected as if it was tapped.

From docs:

Set the selected menu item ID. This behaves the same as tapping on an item.

Code example:

BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(myNavigationItemListener);
bottomNavigationView.setSelectedItemId(R.id.my_menu_item_id);

IMPORTANT

You MUST have already added all items to the menu (in case you do this programmatically) and set the Listener before calling setSelectedItemId(I believe you want the code in your listener to run when you call this method). If you call setSelectedItemId before adding the menu items and setting the listener nothing will happen.

Ricardo
  • 9,136
  • 3
  • 29
  • 35
  • 7
    This does NOT WORK, it only updates the tab itself, it doesnt trigger a setOnNavigationItemSelectedListener event – TjerkW Sep 27 '17 at 09:24
  • 5
    If you haven't defined the behavior in the `onTabSelected` obviously it won't do anything. Read the docs -> Set the selected menu item ID. This behaves the same as tapping on an item. It's from Android Developers – Ricardo Sep 27 '17 at 09:57
  • 1
    I highly advise you to debug and review what you are doing wrong. I have already implemented three BottomNavigationView Applications and never had a problem with this method. – Ricardo Sep 28 '17 at 08:42
  • @Ricardo When I add this to my fragment, application got hang while entering to that fragment – Subin Babu Jan 10 '18 at 06:56
  • @SubinBabu that is probably due to your implementation in `OnNavigationItemSelectedListener`. The docs clearly say that "setSelectedItemId()` behaves the same as tapping on an item. Your application also hangs when you tap on them? – Ricardo Jan 10 '18 at 10:15
  • @Ricardo its ANR and close the application. Would you help to make the bottom navigation view tab selection from another fragment of same activity – Subin Babu Jan 10 '18 at 10:27
  • sure, use the following method in your fragment: ((MyActivity)getActivity()).myBottomNavigationView.seSelectedItemId(R.id.itemId); Let me know if it fixed your problem – Ricardo Jan 10 '18 at 10:32
  • 1
    In my case wasn't working, because I was creating navigationMenu programatically. During construction the click didn't work. After all items added to menu, calling setSelectedItemId() worked. – Pedro Romão Feb 27 '18 at 10:58
  • Won't trigger for the first item in BottomNavigationView but works for the rest. This is expected behavior though. Because the first item is selected by default at start-up and `setSelectedItemId(itemId)` will trigger `onNavigationItemReselected` instead of `onNavigationItemSelected` – Farid Nov 17 '19 at 03:49
78

To programmatically click on the BottomNavigationBar item you need use:

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

This arranges all the items with their labels correctly.

dianakarenms
  • 2,609
  • 1
  • 22
  • 22
  • 5
    This is an hack regarding the problem. You have methods from the library itself which will let you perform what you need. If you can't then it's because you are doing it wrong – Ricardo Feb 23 '18 at 17:20
51

For those, who still use SupportLibrary < 25.3.0

I'm not sure whether this is a complete answer to this question, but my problem was very similar - I had to process back button press and bring user to previous tab where he was. So, maybe my solution will be useful for somebody:

private void updateNavigationBarState(int actionId){
    Menu menu = bottomNavigationView.getMenu();

    for (int i = 0, size = menu.size(); i < size; i++) {
        MenuItem item = menu.getItem(i);
        item.setChecked(item.getItemId() == actionId);
    }
}

Please, keep in mind that if user press other navigation tab BottomNavigationView won't clear currently selected item, so you need to call this method in your onNavigationItemSelected after processing of navigation action:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.some_id_1:
            // process action
            break;
        case R.id.some_id_2:
            // process action
            break;
        ...
        default:
            return false;
    }

    updateNavigationBarState(item.getItemId());

    return true;
}

Regarding the saving of instance state I think you could play with same action id of navigation view and find suitable solution.

Viacheslav
  • 5,443
  • 1
  • 29
  • 36
  • Doesn´t `Menu menu = bottomNavigationView.getMenu();` return a copy of the menu, so the `bottomNavigationView` object is not affected? – 最白目 Feb 16 '17 at 08:14
  • 1
    @dan According to source it returns class attribute mMenu, not the copy https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/BottomNavigationView.java?autodive=0%2F%2F%2F%2F%2F – Viacheslav Feb 16 '17 at 08:16
  • 6
    You don't need to uncheck other items, at least on 26.1.0/27.1.0 – Jemshit Mar 19 '18 at 11:38
  • don't do uncheck, it will highlight them – djdance Mar 10 '19 at 08:40
44
bottomNavigationView.setSelectedItemId(R.id.action_item1);

where action_item1 is menu item ID.

CinCout
  • 9,486
  • 12
  • 49
  • 67
Ashwini
  • 653
  • 6
  • 7
17

use

        bottomNavigationView.getMenu().getItem(POSITION).setChecked(true);
behrad
  • 1,228
  • 14
  • 21
17

Use this to set selected bottom navigation menu item by menu id

MenuItem item = mBottomNavView.getMenu().findItem(menu_id);
item.setChecked(true);
vhad01
  • 171
  • 1
  • 5
9
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

   bottomNavigationView.setOnNavigationItemSelectedListener(this);
   Menu menu = bottomNavigationView.getMenu();
   this.onNavigationItemSelected(menu.findItem(R.id.action_favorites));
}
Rob
  • 26,989
  • 16
  • 82
  • 98
Askar Syzdykov
  • 399
  • 3
  • 15
9

It is now possible since 25.3.0 version to call setSelectedItemId() \o/

Boy
  • 7,010
  • 4
  • 54
  • 68
7

Add android:enabled="true" to BottomNavigationMenu Items.

And then set bottomNavigationView.setOnNavigationItemSelectedListener(mListener) and set it as selected by doing bottomNavigationView.selectedItemId = R.id.your_menu_id

7

You can try the performClick method :

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

Edit

From API 25.3.0 it was introduced the method setSelectedItemId(int id) which lets you mark an item as selected as if it was tapped.

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
6
navigationView.getMenu().findItem(R.id.navigation_id).setChecked(true);
Reza Abiri
  • 73
  • 1
  • 7
4

This will probably be added in coming updates. But in the meantime, to accomplish this you can use reflection.

Create a custom view extending from BottomNavigationView and access some of its fields.

public class SelectableBottomNavigationView extends BottomNavigationView {

    public SelectableBottomNavigationView(Context context) {
        super(context);
    }

    public SelectableBottomNavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectableBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setSelected(int index) {
        try {
            Field f = BottomNavigationView.class.getDeclaredField("mMenuView");
            f.setAccessible(true);
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) f.get(this);

            try {
                Method method = menuView.getClass().getDeclaredMethod("activateNewButton", Integer.TYPE);
                method.setAccessible(true);
                method.invoke(menuView, index);
            } catch (SecurityException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

}

And then use it in your xml layout file.

<com.your.app.SelectableBottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemBackground="@color/primary"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:menu="@menu/bottom_navigation_menu"/>
cpienovi
  • 410
  • 4
  • 7
4

I you don't want to modify your code. If so, I recommended you to try BottomNavigationViewEx

You just need replace call a method setCurrentItem(index); and getCurrentItem()

Click here to view the image

ittianyu
  • 509
  • 4
  • 7
4

Just adding another way to perform a selection programatically - this is probably what was the intention in the first place or maybe this was added later on.

Menu bottomNavigationMenu = myBottomNavigationMenu.getMenu();
bottomNavigationMenu.performIdentifierAction(selected_menu_item_id, 0);

The performIdentifierAction takes a Menu item id and a flag.

See the documentation for more info.

Darwind
  • 7,284
  • 3
  • 49
  • 48
  • 1
    This seems to perform the action belonging to the menu item, but doesn't cause the menu item to appear selected. I thought the latter was what the OP wanted, but I'm not positive. – LarsH Dec 08 '16 at 05:19
4

Seems to be fixed in SupportLibrary 25.1.0 :) Edit: It seems to be fixed, that the state of the selection is saved, when rotating the screen.

Michael
  • 2,021
  • 3
  • 12
  • 18
4

Above API 25 you can use setSelectedItemId(menu_item_id) but under API 25 you must do differently, user Menu to get handle and then setChecked to Checked specific item

mhheydarchi
  • 79
  • 1
  • 5
2

I made a bug to Google about the fact that there's no reliable way to select the page on a BottomNavigationView: https://code.google.com/p/android/issues/detail?id=233697

NavigationView apparently had a similar issue, which they fixed by adding a new setCheckedItem() method.

Johan Paul
  • 2,203
  • 2
  • 22
  • 38
2

I hope this helps

//Setting default selected menu item and fragment
        bottomNavigationView.setSelectedItemId(R.id.action_home);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, new HomeFragment()).commit();

It is more of determining the default fragment loaded at the same time with the corresponding bottom navigation menu item. You can include the same in your OnResume callbacks

Zephania Mwando
  • 118
  • 1
  • 9
1

To change the Tab, this code works!

    activity?.supportFragmentManager?.beginTransaction().also { fragmentTransaction ->
        fragmentTransaction?.replace(R.id.base_frame, YourFragment())?.commit()
    }

    val bottomNavigationView: BottomNavigationView = activity?.findViewById(R.id.bottomNavigationView) as BottomNavigationView
    bottomNavigationView.menu.findItem(R.id.navigation_item).isChecked = true
Diego Salcedo
  • 83
  • 1
  • 7
1

in Kotlin: bottomNavigationView.menu.getItem(POSITION).isChecked = true

SilverTech
  • 399
  • 4
  • 11
0

Reflection is bad idea.

Head to this gist. There is a method that performs the selection but also invokes the callback:

  @CallSuper
    public void setSelectedItem(int position) {
        if (position >= getMenu().size() || position < 0) return;

        View menuItemView = getMenuItemView(position);
        if (menuItemView == null) return;
        MenuItemImpl itemData = ((MenuView.ItemView) menuItemView).getItemData();


        itemData.setChecked(true);

        boolean previousHapticFeedbackEnabled = menuItemView.isHapticFeedbackEnabled();
        menuItemView.setSoundEffectsEnabled(false);
        menuItemView.setHapticFeedbackEnabled(false); //avoid hearing click sounds, disable haptic and restore settings later of that view
        menuItemView.performClick();
        menuItemView.setHapticFeedbackEnabled(previousHapticFeedbackEnabled);
        menuItemView.setSoundEffectsEnabled(true);


        mLastSelection = position;

    }
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • I use `callOnClick()` instead of `performClick()`, this way I don't need to disable sound, which didn't work with `performClick()` anyway. – arekolek Mar 14 '17 at 15:08
0
private void setSelectedItem(int actionId) {
    Menu menu = viewBottom.getMenu();
    for (int i = 0, size = menu.size(); i < size; i++) {
        MenuItem menuItem = menu.getItem(i);
        ((MenuItemImpl) menuItem).setExclusiveCheckable(false);
        menuItem.setChecked(menuItem.getItemId() == actionId);
        ((MenuItemImpl) menuItem).setExclusiveCheckable(true);
    }
}

The only 'minus' of the solution is using MenuItemImpl, which is 'internal' to library (though public).

bibi
  • 3,671
  • 5
  • 34
  • 50
Alexey
  • 2,980
  • 4
  • 30
  • 53
0

IF YOU NEED TO DYNAMICALLY PASS FRAGMENT ARGUMENTS DO THIS

There are plenty of (mostly repeated or outdated) answers here but none of them handles a very common need: dynamically passing different arguments to the Fragment loaded into a tab.

You can't dynamically pass different arguments to the loaded Fragment by using setSelectedItemId(R.id.second_tab), which ends up calling the static OnNavigationItemSelectedListener. To overcome this limitation I've ended up doing this in my MainActivity that contains the tabs:

fun loadArticleTab(articleId: String) {
    bottomNavigationView.menu.findItem(R.id.tab_article).isChecked = true // use setChecked() in Java
    supportFragmentManager
        .beginTransaction()
        .replace(R.id.main_fragment_container, ArticleFragment.newInstance(articleId))
        .commit()
}

The ArticleFragment.newInstance() method is implemented as usual:

private const val ARG_ARTICLE_ID = "ARG_ARTICLE_ID"

class ArticleFragment : Fragment() {

    companion object {
        /**
         * @return An [ArticleFragment] that shows the article with the given ID.
         */
        fun newInstance(articleId: String): ArticleFragment {
            val args = Bundle()
            args.putString(ARG_ARTICLE_ID, day)
            val fragment = ArticleFragment()
            fragment.arguments = args
            return fragment
        }
    }

}
Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
0

This method work for me.

private fun selectBottomNavigationViewMenuItem(bottomNavigationView : BottomNavigationView,@IdRes menuItemId: Int) {
            bottomNavigationView.setOnNavigationItemSelectedListener(null)
            bottomNavigationView.selectedItemId = menuItemId
            bottomNavigationView.setOnNavigationItemSelectedListener(this)
        }

Example

 override fun onBackPressed() {
        replaceFragment(HomeFragment())
        selectBottomNavigationViewMenuItem(navView, R.id.navigation_home)
    }



private fun replaceFragment(fragment: Fragment) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frame_container, fragment)
        transaction.commit()
    }
code4rox
  • 941
  • 9
  • 34