Add Notification Badge in Android

Notification Badge is a simple feature, but it’s hugely useful because it lets you know exactly which apps need your attention, and how much attention they need.

For some reason, Google has yet to create a similar badge system in Android. Instead, notifications are collected in the notification panel, so users don’t know how many notifications they have from each app until they swipe open the panel and look.
Alternative : Make Badge(Item count) in Android
Today i am telling you about a library which can help to create notification badge with animation.

add notification badge in android

Gradle

compile 'com.nex3z:notification-badge:0.1.0'

Usage

Use setNumber(int number) or setText(String text) to add number or arbitrary text on the badge. Use clear() to clear the badge.

Downlaod library

First of all you need to define that badge in

AndroidManifest.xml

<application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
  <intent-filter>
   <action android:name="android.intent.action.MAIN"/>

   <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
 </activity>
</application>
Now create a layout where i notify the action and for this demo i am creating three button -->> Increase, Many Notification and Clear

activity_main.xml

<ImageView
 android:id="@+id/icon"
 android:layout_width="48dp"
 android:layout_height="48dp"
 android:layout_marginTop="8dp"
 android:src="@drawable/ic_chat_24dp"/>

<com.nex3z.notificationbadge.NotificationBadge
 android:id="@+id/badge"
 android:layout_width="28dp"
 android:layout_height="28dp"
 android:layout_toRightOf="@id/icon"
 android:layout_alignTop="@id/icon"
 android:layout_marginLeft="-16dp"
 android:layout_marginTop="-8dp"
 app:badgeBackground="@drawable/badge_bg_with_shadow"
 app:maxTextLength="2"/>

<Button
 android:id="@+id/increase"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/icon"
 android:text="@string/increase"/>

<Button
 android:id="@+id/many"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/increase"
 android:text="@string/many"/>

<Button
 android:id="@+id/clear"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/many"
 android:text="@string/clear"/>
And finally this time to perform action by

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.nex3z.notificationbadge.NotificationBadge;

public class MainActivity extends AppCompatActivity {

    NotificationBadge mBadge;
    private int mCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBadge = (NotificationBadge) findViewById(R.id.badge);

        Button increase = (Button) findViewById(R.id.increase);
        increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBadge.setNumber(++mCount);
            }
        });

        Button many = (Button) findViewById(R.id.many);
        many.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCount = 98;
                mBadge.setNumber(mCount);
            }
        });

        Button clear = (Button) findViewById(R.id.clear);
        clear .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCount = 0;
                mBadge.setNumber(mCount);
            }
        });
    }
}

I hope you got the point and enjoy coding.

Comments