0

I'm trying to implement notification badge count to my notification tab on ViewPager. I made my badge a TextView but I'm not sure where I have to add it to. I will post my codes below.

Currently, this is how my notification tab looks like.

enter image description here

I followed this example: Add new item count to icon on button - Android

tab_item_notification.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">


        <item
            android:id="@+id/test"
            android:state_selected="true"
            android:drawable="@drawable/ic_notifications_active_white_24dp" />


        <item
            android:id="@+id/test2"
            android:state_selected="false"
            android:drawable="@drawable/ic_notifications_active_black_24dp" />

    </selector>

ic_notifications_active_black_24dp.xml

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24.0"
            android:viewportHeight="24.0">
        <path
            android:fillColor="#BDBDBD"
            android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42zM18,11c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2v-5zM12,22c0.14,0 0.27,-0.01 0.4,-0.04 0.65,-0.14 1.18,-0.58 1.44,-1.18 0.1,-0.24 0.15,-0.5 0.15,-0.78h-4c0.01,1.1 0.9,2 2.01,2z"/>


    </vector>

badge_circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#F00" />
    <stroke
        android:width="2dip"
        android:color="#FFF" />
    <padding
        android:left="5dip"
        android:right="5dip"
        android:top="5dip"
        android:bottom="5dip" />
</shape>

I am trying to implement this but I am not sure where do I add this to. Does anybody where to add?

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:text="10"
  android:textColor="#FFF"
  android:textSize="16sp"
  android:textStyle="bold"
  android:background="@drawable/badge_circle"/>
arsenallavigne
  • 131
  • 1
  • 2
  • 16

1 Answers1

0

1) You can use a a custom TabLayout with badge support for Tabs

Here is the Gist

use it like:

yourBadgetTabLayoutObject.with(desireTabPosition).badge(true).badgeCount(1).build();

2) You can create a custom tabview for a particular position and set the view using:

TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));

For Details see this Answer

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62