Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display count of notifications in toolbar icon in android

I would like to make an icon counter for android just like the cart. I have seen many e-commerce app cart icon count increase. I show flipkart app snapshot.:-

enter image description here

like image 514
Anand Jain Avatar asked Nov 16 '15 06:11

Anand Jain


People also ask

How do I display the number of notifications in Android app launcher icon?

If you want to change badge with number, you can be changed in NOTIFICATION SETTING on the notification panel or Settings > Notifications > App icon badges > Select Show with number.

How do I get app icon notifications on Android?

Navigate back to the main Settings screen, tap Notifications, and then tap Advanced settings. Tap the switch next to App icon badges to turn them on.

What is status bar in notification?

Status bar (or notification bar) is an interface element at the top of the screen on Android devices that displays the notification icons, minimized notifications, battery information, device time, and other system status details.


1 Answers

In my solution, whenever a new notification arrives, the counter will increase (as observed in shopping apps)

Try this, it works on my MOTO e2.

Make sure your API Level > 14

Create a layout like:

</RelativeLayout>
    <ImageView
        android:id="@+id/counterBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/unread_background" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="8sp"
        android:layout_centerInParent="true"
        android:textColor="#FFFFFF" />
</RelativeLayout>

In onCreateOptionMenu,

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.menu_main, menu);

   MenuItem menuItem = menu.findItem(R.id.testAction);
   menuItem.setIcon(buildCounterDrawable(count,  R.drawable.ic_menu_gallery));

   return true;
}

Now, build method for Icon :

private Drawable buildCounterDrawable(int count, int backgroundImageId) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
    view.setBackgroundResource(backgroundImageId);

    if (count == 0) {
        View counterTextPanel = view.findViewById(R.id.counterValuePanel);
        counterTextPanel.setVisibility(View.GONE);
    } else {
        TextView textView = (TextView) view.findViewById(R.id.count);
        textView.setText("" + count);
    }

    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return new BitmapDrawable(getResources(), bitmap);
}

You can refer from here

like image 171
Gaurav Setia Avatar answered Oct 04 '22 07:10

Gaurav Setia