aurelhubert / ahbottomnavigation

A library to reproduce the behavior of the Bottom Navigation guidelines from Material Design.
3.84k stars 682 forks source link

Custom drawable as badge #198

Open emartynov opened 7 years ago

emartynov commented 7 years ago

We want to show not a number but just an image as the badge. For example, an exclamation mark as error or spinner as a progress indicator. I've tried a workaround to pass drawable as a background of the badge and the empty string as badge text. That doesn't work well since it is stretching the drawable:

screenshot 2016-12-23 09 55 34

Are there good solution for our problem?

emartynov commented 7 years ago

I ended up by adding ImageView to navigation item. But code looks quite verbose:

    Resources resources = getResources();
    int imageSize = (int) resources.getDimension(R.dimen.main__notification_icon_size);
    int leftMargin = (int) resources.getDimension(R.dimen.bottom_navigation_notification_margin_left);

    notificationIcon = new ImageView(this);

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(imageSize, imageSize);
    params.setMargins(leftMargin, 0, 0, 0);
    params.gravity = Gravity.CENTER_HORIZONTAL;
    notificationIcon.setLayoutParams(params);

    notificationIcon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_icon_outdate_warning));
    ViewGroup content = (ViewGroup) bottomNavigation.getChildAt(1);
    ViewGroup item = (ViewGroup) content.getChildAt(index);
    item.addView(notificationIcon);
emartynov commented 7 years ago

My code has a bug. For the KitKat and below first child is actually LinearLayout that holds items view. And for Lollipop and up the code bottomNavigation.getChildAt(1) is correct since the child 0 is background