How to change selected tab of BottomNavigationView with code?

4.1k views Asked by At

Is there anyway to change selected tab of BottomNavigationView from "com.android.support:design" library with code?

    <android.support.design.widget.BottomNavigationView
        xmlns:design="http://schema.android.com/apk/res/android.support.design"
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_anchorGravity="bottom"
        app:menu="@menu/bottom_bar"
        design:menu="@menu/bottom_bar"/>
3

There are 3 answers

2
Sinan Kozak On BEST ANSWER

Looks like we need to update BottomNavigaitonPresenter's state with updateMenuView() method. Example code with reflection;

                        bottomNavigationView.getMenu().getItem(3).setChecked(false);
                    bottomNavigationView.getMenu().getItem(2).setChecked(false);
                    bottomNavigationView.getMenu().getItem(1).setChecked(false);
                    bottomNavigationView.getMenu().getItem(0).setChecked(true);

                    try {
                        final Field mPresenter = bottomNavigationView.getClass().getField("mPresenter");
                        mPresenter.setAccessible(true);

                        final BottomNavigationPresenter presenter = (BottomNavigationPresenter) mPresenter.get(bottomNavigationView);
                        presenter.updateMenuView(true);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
2
tysheng On

Here is my code for position BottomNavigationView.

/**
 * Created by tysheng
 * Date: 2016/12/31 12:24.
 * Email: [email protected]
 */

public class PositionBottomNavigationView extends BottomNavigationView implements BottomNavigationView.OnNavigationItemSelectedListener {
    private SparseIntArray mMenuIds;
    private int currentPosition;
    private onPositionSelectedListener mListener;

    public PositionBottomNavigationView(Context context) {
        this(context, null, 0);
    }

    public PositionBottomNavigationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PositionBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mMenuIds = new SparseIntArray();
        setOnNavigationItemSelectedListener(this);
    }

    /**
     * ATTENTION: register the ids first.
     *
     * @param ids menu ids by order, like R.id.menu_0,R.id.menu_1...
     */
    public void registerIds(int... ids) {
        mMenuIds.clear();
        for (int i = 0; i < ids.length; i++) {
            mMenuIds.put(i, ids[i]);
        }
    }

    public void setOnPositionSelectedListener(onPositionSelectedListener listener) {
        mListener = listener;
    }

    public interface onPositionSelectedListener {
        void onPositionSelected(int position);
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
        setPositionChecked();
        if (mListener != null)
            mListener.onPositionSelected(currentPosition);
    }

    private void setPositionChecked() {
        Menu menu = getMenu();
        if (menu.getItem(currentPosition).isChecked()) {
            return;
        }
        for (int i = 0; i < mMenuIds.size(); i++) {
            if (mMenuIds.get(i) != mMenuIds.get(currentPosition)) {
                menu.getItem(i).setChecked(false);
            } else {
                menu.getItem(i).setChecked(true);
            }
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int pos = mMenuIds.indexOfValue(item.getItemId());
        if (pos != -1) {
            setCurrentPosition(pos);
        }
        return true;
    }
}
1
Rahul Ravindran On

This is the correct way to do it :

To programmatically click on the BottomNavigationBar item you need use:

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

This not only programmatically clicks the BottomNavigationBar but also arranges the labels correctly.