Christine
Christine

Reputation: 329

Android how to add badge to button

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"/>

Upvotes: 9

Views: 16255

Answers (3)

Moumen Lahmidi
Moumen Lahmidi

Reputation: 482

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

Upvotes: 6

Amitabh Sarkar
Amitabh Sarkar

Reputation: 1311

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

Upvotes: 2

EE66
EE66

Reputation: 4651

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.

Upvotes: 2

Related Questions