Java Code Examples for com.google.android.material.bottomnavigation.BottomNavigationView#getOrCreateBadge()

The following examples show how to use com.google.android.material.bottomnavigation.BottomNavigationView#getOrCreateBadge() . 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 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 3
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);


}