com.google.android.material.badge.BadgeDrawable Java Examples

The following examples show how to use com.google.android.material.badge.BadgeDrawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BottomNavigationDemoFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private void setupBadging() {
  for (BottomNavigationView bn : bottomNavigationViews) {
    int menuItemId = bn.getMenu().getItem(0).getItemId();
    // An icon only badge will be displayed.
    BadgeDrawable badge = bn.getOrCreateBadge(menuItemId);
    badge.setVisible(true);

    menuItemId = bn.getMenu().getItem(1).getItemId();
    // A badge with the text "99" will be displayed.
    badge = bn.getOrCreateBadge(menuItemId);
    badge.setVisible(true);
    badge.setNumber(99);

    menuItemId = bn.getMenu().getItem(2).getItemId();
    // A badge with the text "999+" will be displayed.
    badge = bn.getOrCreateBadge(menuItemId);
    badge.setVisible(true);
    badge.setNumber(9999);
  }
}
 
Example #2
Source File: BottomNavigationDemoFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private void clearAndHideBadge(int menuItemId) {
  for (BottomNavigationView bn : bottomNavigationViews) {
    MenuItem menuItem = bn.getMenu().getItem(0);
    if (menuItem.getItemId() == menuItemId) {
      // Hide instead of removing the badge associated with the first menu item because the user
      // can trigger it to be displayed again.
      BadgeDrawable badgeDrawable = bn.getBadge(menuItemId);
      if (badgeDrawable != null) {
        badgeDrawable.setVisible(false);
        badgeDrawable.clearNumber();
      }
    } else {
      // Remove the badge associated with this menu item because cannot be displayed again.
      bn.removeBadge(menuItemId);
    }
  }
}
 
Example #3
Source File: TeiDashboardMobileActivity.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void updateNoteBadge(int numberOfNotes) {
    if (binding.tabLayout.getTabCount() > 0) {
        BadgeDrawable badge = binding.tabLayout.getTabAt(getLastTabPosition()).getOrCreateBadge();
        badge.setVisible(numberOfNotes > 0);
        badge.setBackgroundColor(Color.WHITE);
        badge.setBadgeTextColor(ColorUtils.getPrimaryColor(getContext(), ColorUtils.ColorType.PRIMARY));
        badge.setNumber(numberOfNotes);
        badge.setMaxCharacterCount(3);
    }
}
 
Example #4
Source File: MainActivity.java    From ui with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navView = findViewById(R.id.nav_view);

    /*  if not using arch navigation, then you need to implement this.
    navView.setOnNavigationItemSelectedListener(
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            public boolean onNavigationItemSelected(MenuItem item) {
                return false;
            }
        }

    );
    */

    // Passing each menu ID as a set of Ids because each menu should be considered as top level destinations.
    //Note for this to work with arch Navigation, these id must be the same id in menu.xml and the nav_graph.
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.action_first, R.id.action_second, R.id.action_third)
        .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

    //In order to have badges, you need to use the Theme.MaterialComponents.DayNight  (doesn't have to daynight, but materialcompents).
    BadgeDrawable badge = navView.getOrCreateBadge(R.id.action_second);
    badge.setNumber(12);  //should show a 12 in the "badge" for the second one.
    badge.setVisible(true);


}
 
Example #5
Source File: QueueActivity.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private void onErrorsChanged(PagedList<Content> result) {
    // Update errors tab
    if (result.isEmpty()) errorsTab.removeBadge();
    else {
        BadgeDrawable badge = errorsTab.getOrCreateBadge();
        badge.setVisible(true);
        badge.setNumber(result.size());
    }
}
 
Example #6
Source File: QueueActivity.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private void onQueueChanged(List<QueueRecord> result) {
    // Update queue tab
    if (result.isEmpty()) queueTab.removeBadge();
    else {
        BadgeDrawable badge = queueTab.getOrCreateBadge();
        badge.setVisible(true);
        badge.setNumber(result.size());
    }
}
 
Example #7
Source File: TabLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of {@link BadgeDrawable} if none exists. Initializes (if needed) and
 * returns the associated instance of {@link BadgeDrawable}.
 *
 * @return an instance of BadgeDrawable associated with {@code Tab}.
 */
@NonNull
private BadgeDrawable getOrCreateBadge() {
  // Creates a new instance if one is not already initialized for this TabView.
  if (badgeDrawable == null) {
    badgeDrawable = BadgeDrawable.create(getContext());
  }
  tryUpdateBadgeAnchor();
  if (badgeDrawable == null) {
    throw new IllegalStateException("Unable to create badge");
  }
  return badgeDrawable;
}
 
Example #8
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void setBadgeIfNeeded(@NonNull BottomNavigationItemView child) {
  int childId = child.getId();
  if (!isValidId(childId)) {
    // Child doesn't have a valid id, do not set any BadgeDrawable on the view.
    return;
  }

  BadgeDrawable badgeDrawable = badgeDrawables.get(childId);
  if (badgeDrawable != null) {
    child.setBadge(badgeDrawable);
  }
}
 
Example #9
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
void removeBadge(int menuItemId) {
  validateMenuItemId(menuItemId);
  BadgeDrawable badgeDrawable = badgeDrawables.get(menuItemId);
  BottomNavigationItemView itemView = findItemView(menuItemId);
  if (itemView != null) {
    itemView.removeBadge();
  }
  if (badgeDrawable != null) {
    badgeDrawables.remove(menuItemId);
  }
}
 
Example #10
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of {@link BadgeDrawable} if none exists. Initializes (if needed) and
 * returns the associated instance of {@link BadgeDrawable}.
 *
 * @param menuItemId Id of the menu item.
 * @return an instance of BadgeDrawable associated with {@code menuItemId}.
 */
BadgeDrawable getOrCreateBadge(int menuItemId) {
  validateMenuItemId(menuItemId);
  BadgeDrawable badgeDrawable = badgeDrawables.get(menuItemId);
  // Create an instance of BadgeDrawable if none were already initialized for this menu item.
  if (badgeDrawable == null) {
    badgeDrawable = BadgeDrawable.create(getContext());
    badgeDrawables.put(menuItemId, badgeDrawable);
  }
  BottomNavigationItemView itemView = findItemView(menuItemId);
  if (itemView != null) {
    itemView.setBadge(badgeDrawable);
  }
  return badgeDrawable;
}
 
Example #11
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
void setBadgeDrawables(SparseArray<BadgeDrawable> badgeDrawables) {
  this.badgeDrawables = badgeDrawables;
  if (buttons != null) {
    for (BottomNavigationItemView itemView : buttons) {
      itemView.setBadge(badgeDrawables.get(itemView.getId()));
    }
  }
}
 
Example #12
Source File: TabsMainDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void updateBadgeGravity(@BadgeGravity int badgeGravity) {
  for (TabLayout tabLayout : tabLayouts) {
    // Update the badge gravity on all the tabs.
    for (int index = 0; index < tabLayout.getTabCount(); index++) {
      BadgeDrawable badgeDrawable = tabLayout.getTabAt(index).getBadge();
      if (badgeDrawable != null) {
        badgeDrawable.setBadgeGravity(badgeGravity);
      }
    }
  }
}
 
Example #13
Source File: EventCaptureActivity.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void updateNoteBadge(int numberOfNotes) {
    BadgeDrawable badge = binding.eventTabLayout.getTabAt(binding.eventTabLayout.getTabCount() - 1).getOrCreateBadge();
    badge.setVisible(numberOfNotes > 0);
    badge.setBackgroundColor(Color.WHITE);
    badge.setBadgeTextColor(ColorUtils.getPrimaryColor(getContext(), ColorUtils.ColorType.PRIMARY));
    badge.setNumber(numberOfNotes);
    badge.setMaxCharacterCount(3);
}
 
Example #14
Source File: BottomNavigationDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void updateBadgeNumber(int delta) {
  for (BottomNavigationView bn : bottomNavigationViews) {
    // Increase the badge number on the first menu item.
    MenuItem menuItem = bn.getMenu().getItem(0);
    int menuItemId = menuItem.getItemId();
    BadgeDrawable badgeDrawable = bn.getOrCreateBadge(menuItemId);
    // In case the first menu item has been selected and the badge was hidden, call
    // BadgeDrawable#setVisible() to ensure the badge is visible.
    badgeDrawable.setVisible(true);
    badgeDrawable.setNumber(badgeDrawable.getNumber() + delta);
  }
}
 
Example #15
Source File: BottomNavigationDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void updateBadgeGravity(@BadgeGravity int badgeGravity) {
  for (BottomNavigationView bn : bottomNavigationViews) {
    for (int i = 0; i < MAX_BOTTOM_NAV_CHILDREN; i++) {
      // Update the badge gravity on all the menu items.
      MenuItem menuItem = bn.getMenu().getItem(i);
      int menuItemId = menuItem.getItemId();
      BadgeDrawable badgeDrawable = bn.getBadge(menuItemId);
      if (badgeDrawable != null) {
        badgeDrawable.setBadgeGravity(badgeGravity);
      }
    }
  }
}
 
Example #16
Source File: TabsMainDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void incrementBadgeNumber() {
  for (TabLayout tabLayout : tabLayouts) {
    // Increase the badge number on the first tab position.
    // In case the first tab has been selected and the badge was hidden, call
    // BadgeDrawable#setVisible() to ensure the badge is visible.
    BadgeDrawable badgeDrawable = tabLayout.getTabAt(0).getOrCreateBadge();
    badgeDrawable.setVisible(true);
    badgeDrawable.setNumber(badgeDrawable.getNumber() + 1);
  }
}
 
Example #17
Source File: TabsMainDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void clearAndHideBadge(int tabPosition) {
  for (TabLayout tabLayout : tabLayouts) {
    if (tabPosition == 0) {
      // Hide instead of removing the badge associated with the first menu item because the user
      // can trigger it to be displayed again.
      BadgeDrawable badgeDrawable = tabLayout.getTabAt(tabPosition).getBadge();
      if (badgeDrawable != null) {
        badgeDrawable.setVisible(false);
        badgeDrawable.clearNumber();
      }
    } else {
      tabLayout.getTabAt(tabPosition).removeBadge();
    }
  }
}
 
Example #18
Source File: TabsControllableDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void setupBadging(TabLayout tabLayout) {
  BadgeDrawable badgeDrawable = tabLayout.getTabAt(0).getOrCreateBadge();
  badgeDrawable.setVisible(true);
  badgeDrawable.setNumber(1);

  badgeDrawable = tabLayout.getTabAt(1).getOrCreateBadge();
  badgeDrawable.setVisible(true);
  badgeDrawable.setNumber(88);

  badgeDrawable = tabLayout.getTabAt(2).getOrCreateBadge();
  badgeDrawable.setVisible(true);
  badgeDrawable.setNumber(999);
}
 
Example #19
Source File: BottomNavigationPresenter.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onRestoreInstanceState(Parcelable state) {
  if (state instanceof SavedState) {
    menuView.tryRestoreSelectedItemId(((SavedState) state).selectedItemId);
    SparseArray<BadgeDrawable> badgeDrawables =
        BadgeUtils.createBadgeDrawablesFromSavedStates(
            menuView.getContext(), ((SavedState) state).badgeSavedStates);
    menuView.setBadgeDrawables(badgeDrawables);
  }
}
 
Example #20
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Nullable
BadgeDrawable getBadge(int menuItemId) {
  return badgeDrawables.get(menuItemId);
}
 
Example #21
Source File: BottomNavigationItemView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
void setBadge(@NonNull BadgeDrawable badgeDrawable) {
  this.badgeDrawable = badgeDrawable;
  if (icon != null) {
    tryAttachBadgeToAnchor(icon);
  }
}
 
Example #22
Source File: TabLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an instance of {@link BadgeDrawable} associated with this tab, null if none was
 * initialized.
 */
@Nullable
public BadgeDrawable getBadge() {
  return view.getBadge();
}
 
Example #23
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
SparseArray<BadgeDrawable> getBadgeDrawables() {
  return badgeDrawables;
}
 
Example #24
Source File: TabLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Nullable
private BadgeDrawable getBadge() {
  return badgeDrawable;
}
 
Example #25
Source File: BottomNavigationItemView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Nullable
BadgeDrawable getBadge() {
  return this.badgeDrawable;
}
 
Example #26
Source File: TabLayout.java    From material-components-android with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an instance of {@link BadgeDrawable} if none exists. Initializes (if needed) and
 * returns the associated instance of {@link BadgeDrawable}.
 *
 * @return an instance of BadgeDrawable associated with {@code Tab}.
 */
@NonNull
public BadgeDrawable getOrCreateBadge() {
  return view.getOrCreateBadge();
}
 
Example #27
Source File: BottomNavigationView.java    From material-components-android with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an instance of {@link BadgeDrawable} associated with {@code menuItemId} if none exists.
 * Initializes (if needed) and returns the associated instance of {@link BadgeDrawable} associated
 * with {@code menuItemId}.
 *
 * @param menuItemId Id of the menu item.
 * @return an instance of BadgeDrawable associated with {@code menuItemId}.
 */
public BadgeDrawable getOrCreateBadge(int menuItemId) {
  return menuView.getOrCreateBadge(menuItemId);
}
 
Example #28
Source File: BottomNavigationView.java    From material-components-android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an instance of {@link BadgeDrawable} associated with {@code menuItemId}, null if none
 * was initialized.
 *
 * @param menuItemId Id of the menu item.
 * @return an instance of BadgeDrawable associated with {@code menuItemId} or null.
 * @see #getOrCreateBadge(int)
 */
@Nullable
public BadgeDrawable getBadge(int menuItemId) {
  return menuView.getBadge(menuItemId);
}