How can I increment badge count on Android App?

Viewed 30

I am using ShortcutBadger to show the badge number manually above app icons.

val badgeCount = 1;
try {
    ShortcutBadger.applyCountOrThrow(this, badgeCount)
    Log.d("ShortcutBadger Success:", badgeCount.toString())
} catch (e: ShortcutBadgeException) {
    Log.d("ShortcutBadger Fail:", e.toString())
}
  1. The example above set badgeCount as "1" but how can I get the current badgeCount. I would like to increment the badge number.

  2. There are devices that does not support badge number anymore as far as I know. Is there a way to add a notification dot instead of adding badge count?

1 Answers

Your can do it without any implementation, here XML code where you specify the number

        <ImageView
        android:id="@+id/notifButton"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginEnd="10dp"
        android:background="@drawable/ic_circle_solid"
        android:backgroundTint="@color/quantum_grey100"
        android:padding="7dp"
        android:src="@drawable/ic_bell"
        app:layout_constraintBottom_toBottomOf="@id/nomFragment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/nomFragment"
        app:tint="@color/black" />

        <TextView
        android:id="@+id/notifMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0"
        android:background="@drawable/rounded_background_red"
        android:backgroundTint="@color/heart_red"
        android:paddingHorizontal="3dp"
        android:textColor="@color/white"
        android:textSize="11sp"
        app:layout_constraintEnd_toEndOf="@+id/notifButton"
        app:layout_constraintTop_toTopOf="@+id/notifButton" />

In your java code :

        notifMenu.setAlpha(1.0f);
        if (number < 99) {
           notifMenu.setText(number);
        } else {
           notifMenu.setText("+99");
        }

Here is bg_rounded:

<?xml version="1.0" encoding="utf-8"?>
<shape 
 xmlns:android="http://schemas.android.com/apk/res/android">
<solid
    android:angle="270"
    android:color="@color/quantum_grey100" />
    <corners
    android:bottomLeftRadius="20dp"
    android:bottomRightRadius="20dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp" />
</shape>
Related