How to disable BottomNavigationView click and Touch?
Asked Answered
D

6

14

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

Division answered 3/10, 2017 at 8:4 Comment(2)
add your code hereTremendous
i just want to impliment, but do not know how,Division
I
26

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);
    }
}
Isoclinal answered 20/11, 2018 at 9:58 Comment(2)
mBottomMenu.menu.iterator().forEach { it.isEnabled = enable }Sheenasheeny
How can I hide a particular tab for eg) say the home tab needs to be hidden in the landscape mode.Animality
C
12

Kotlin style one-liner:

bottom_navigation.menu.forEach { it.isEnabled = false }
Chine answered 7/3, 2020 at 9:57 Comment(1)
Kotlin one-liner FTW!Yumuk
P
2
<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 }
Pinon answered 3/10, 2017 at 9:44 Comment(2)
i still can click and choose the itemDivision
bottomView.setOnClickListener(null); try this.Pinon
W
1

You can set the touch listeners of its subviews. Example using android-ktx:

bottomNav.children.forEach {
   (it as? ViewGroup)?.children?.forEach {
       it.setOnTouchListener { _, _ -> true } // or null to enable touch again
   }
}
Wash answered 15/3, 2018 at 9:4 Comment(0)
H
0
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);
            }
        }
    }
}
Hasa answered 10/4, 2019 at 23:43 Comment(0)
S
0

You can do something like

bottomNavigation.menu.iterator().forEach { it.isEnabled = !error }
Sheenasheeny answered 24/8, 2021 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.