0

I want to disable clicks on BottomNavigationView when my API results are loading. I have used the code below to disable:

if (bottomNavigationView != null) {
        bottomNavigationView.setClickable(!showLoading);
        bottomNavigationView.setFocusable(!showLoading);
        bottomNavigationView.setEnabled(!showLoading);
        if (showLoading) {
            bottomNavigationView.setOnNavigationItemSelectedListener(null);
        } else {
            bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        }
    }

What this does is that it prevents the NavigationItemSelectedListener being called. This is perfectly fine.

Issue is, it still selects the menu item and changes the color as per the selector.

TIA

sb_269
  • 553
  • 2
  • 7
  • 22

2 Answers2

0

Just hide the bottomNavigation while the data isloading().No use of showing the view if its disabled or you can do some animations like translations from bottom while the data is loading..

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

Figured it out:

This is what my selector looks like

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/grey3" android:state_checked="false" />
<item android:color="@color/blue" />
</selector>

Added all my menu items to a group. Got rid of my previous code to disable and now using the below single line code:

if (bottomNavigationView != null) {
            bottomNavigationView.getMenu().setGroupEnabled(R.id.group1, !showLoading);
//            bottomNavigationView.setClickable(!showLoading);
//            bottomNavigationView.setFocusable(!showLoading);
//            bottomNavigationView.setEnabled(!showLoading);
//            if (showLoading) {
//                bottomNavigationView.setOnNavigationItemSelectedListener(null);
//            } else {
//                bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//            }
        }

And in the layout xml:

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/grey0"
        app:elevation="2dp"
        app:layout_anchorGravity="bottom"
        app:menu="@menu/main_nav"
        app:itemTextColor="@color/bottom_nav"
        app:itemIconTint="@color/bottom_nav" />

Last 2 lines is what makes the visual difference.

Hope this helps someone.

Cheers

sb_269
  • 553
  • 2
  • 7
  • 22