How to hide/clear badge when click on one position of bottom navigation menu in android?

Viewed 1833

I am getting stuck at hiding the badge from NavigationView. When I get any notification added badge with position of BottomNavigationNiew but could not refresh that batch when click the position.

Gradle:

compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.7'
compile 'q.rorbin:badgeview:1.1.0'

MainActivity:

private Badge addBadgeAt(int position, int number) {
        return new QBadgeView(this)
                .setBadgeNumber(number)
                .setGravityOffset(42, 2, true)
                .bindTarget(bottomNavigationView.getBottomNavigationItemView(position)); 
}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    switch (item.getItemId()) {
        case R.id.menu_conection:

            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, ConnectionFragment.newInstance("connection"));
            fragmentTransaction.commit();

            break;
        case R.id.menu_me:
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, MyProfile.newInstance(ARG_TYPE));
            fragmentTransaction.commit();

            break;

        case R.id.menu_notification:

            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, NotificationsFragment.newInstance());
            fragmentTransaction.commit();

            break;
    }

    return true;
}

I just want to removed the badge at the second position when click menu_notification

2 Answers

When you call addBadgeAt function, you need to assign the returned Badge object to a variable:

mBadge = addBadgeAt(2, 1);

Then, when you want to remove the badge, simply call hide method:

mBadge.hide(true);

CAUTION: Seems that call two times the hide method cause the badge to be visible again.

You can check out the docs here: https://github.com/qstumn/BadgeView

Related