9

I want to add a badge in my button, how can i do that ?

enter image description here

Button :

                    <Button
                    android:id="@+id/button"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:background="@drawable/badge_circle"
                    android:gravity="center"
                    android:padding="12dip"
                    android:text="Button"
                    android:textColor="#ffffff"/>
Christine
  • 329
  • 1
  • 4
  • 13

3 Answers3

6

You can use this

BadgeDrawable badgeDrawable =  BadgeDrawable.create(getContext());
badgeDrawable.setNumber(3);
badgeDrawable.setVisible(true);
BadgeUtils.attachBadgeDrawable(badgeDrawable, tragetView);

check for more https://material.io/develop/android/components/badging

Ali Azaz Alam
  • 1,782
  • 1
  • 16
  • 27
Moumen Lahmidi
  • 462
  • 5
  • 7
2

Need to set the compound drawable of the button. Take a look at this and this for better understandment. If u have any more questions. ask.

Community
  • 1
  • 1
EE66
  • 4,601
  • 1
  • 17
  • 20
2

Use this library

Sample example provided by library

View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();

Also check this Question

Amitabh Sarkar
  • 1,281
  • 1
  • 13
  • 26