How to disable BottomNavigationView click and Touch?

Viewed 15123

i want to implement behavior on certain condition the bottom view is unable to click, i want to make if bottom view item being click it does not navigate to that item but still stay at the current item

this is my bottom view layout

6 Answers

You can disable menu items if you want to disable bottom navigation view

private void enableBottomBar(boolean enable){
    for (int i = 0; i < mBottomMenu.getMenu().size(); i++) {
        mBottomMenu.getMenu().getItem(i).setEnabled(enable);
    }
}

Kotlin style one-liner:

bottom_navigation.menu.forEach { it.isEnabled = false }
<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:contextClickable="false"/>

Try this code.it Disables the click.

Dynamicaly using Java pr Kotlin you can disable click.

    bottomView.setEnabled(false); 
    bottomView.setFocusable(false); 
    bottomView.setFocusableInTouchMode(false);
    bottomView.setClickable(false); 
    bottomView.setContextClickable(false);
    bottomView.setOnClickListener(null);

setting onClick Listener to Null helps to Disable click events

bottomView.menu.forEach { it.isEnabled = false }
public class CustomBottomNavigationView extends BottomNavigationView {

    ...

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        ViewGroup menuView = (ViewGroup) getChildAt(0);
        if (menuView != null) {
            for (int i = 0; i < menuView.getChildCount(); i++) {
                menuView.getChildAt(i).setEnabled(enabled);
            }
        }
    }
}

You can do something like

bottomNavigation.menu.iterator().forEach { it.isEnabled = !error }
Related