24

I am trying to add badges to the icons in my android app. e.g. in the facebook app(for iPhone), in the home page the number of pending requests is shown on the requests icon.

Can someone provide any links/ideas on how to do this?

Thanks

Michael_19
  • 4,799
  • 6
  • 24
  • 20

6 Answers6

20

If you really want to do it. Use a FrameLayout which contains an ImageView with your icon and a TextView with a ninepatch drawable as background in the right corner. Add margins to the ImageView if you want the badge to be a bit outside the icon.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
alexanderblom
  • 8,632
  • 6
  • 34
  • 40
  • @alexanderblom - http://alexanderblom.se/gmail-unread-count/ is not working now.. can you please provide a new one.. in fact I wanted this functionality to implement on TabHost – Vishal Khakhkhar Nov 28 '11 at 07:27
8

Thanks Alexanderblom ,for the hints.i used that logic and manage to create badge on a internal imageicon.here is the xml file.and you have to just create a red circle on the drawable.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:text="5"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:padding="2sp"
        android:gravity="center"
        android:background="@drawable/circle"
        android:layout_gravity="top|right" />

</FrameLayout>

and the circle.xml is

<item>
    <shape android:shape="oval">
        <solid android:color="@android:color/black" />
    </shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
    <shape android:shape="oval">
        <solid android:color="@color/Red" />

    </shape>
</item>
ranjit patel
  • 757
  • 1
  • 9
  • 21
7

Android doesn't support badges on icons.

Instead, it provides the notification system (icons in the notification bar, etc), as well as the ability to create widgets that can be added to the users "desktop".

synic
  • 26,359
  • 20
  • 111
  • 149
3

check this library which adds badge to the icon/button/tab

https://github.com/jgilfelt/android-viewbadger

2

Try this one, is good for Sony, Samsung, LG, HTC, Xiaomi, ASUS, ADW, APEX and NOVA Launchers.

https://github.com/leolin310148/ShortcutBadger

Webdma
  • 714
  • 6
  • 16
0

Follow below define steps for add notification count to app icon

Steps:

  1. Add mavenCentral to your build script.

    repositories { mavenCentral() }

  2. Add dependencies in your app gradle.

    dependencies { compile 'me.leolin:ShortcutBadger:1.1.4@aar' or compile 'me.leolin:ShortcutBadger:1.1.3@aar' }

  3. Add the codes below for display notification count on app icon:

    int badgeCount = 1; ShortcutBadger.applyCount(context, badgeCount); //for 1.1.4 Or ShortcutBadger.with(getApplicationContext()).count(badgeCount); //for 1.1.3

  4. If you want to remove the badge

    ShortcutBadger.removeCount(context); //for 1.1.4 ShortcutBadger.with(getApplicationContext()).remove(); //for 1.1.3 Or ShortcutBadger.applyCount(context, 0); //for 1.1.4 ShortcutBadger.with(getApplicationContext()).count(0); //for 1.1.3

Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
Chirag Prajapati
  • 25
  • 1
  • 1
  • 7