Igor
Igor

Reputation: 6255

Creating iPhone-like badge notification on Android

ALL,

Everywhere I look I see a reply on how to make it work for application icon. My situation is a little different.

In my program I have a ListView which displays images. Every image is associated with the object underneath.

What I want to do is create a design like in the iPhone badge notification, but for all those images in the view.

Trying to ask Google, I found this link. Problem is - it does not work. I'm testing on the LG Android phone with 2.2 kernel and all I see is the small red dot which is not even located on the image itself couple of pixels higher and to the left.

Here's my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/user_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/user_image_description"
        android:src="@drawable/placeholder"
    />
    <TextView
        android:id="@+id/user_messages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/icon"
        android:layout_alignBottom="@id/icon"
        android:background="@color/red"
        android:textColor="@color/silver"
        android:layout_marginLeft="-4dp"
        android:layout_marginBottom="-4dp"
        android:textSize="12sp"
        android:textStyle="bold"
        android:gravity="center"
    />
</RelativeLayout>

Can someone please look?

I tried to change the margins as well as text size, but it didn't change anything.

Thank you.

Upvotes: 4

Views: 1275

Answers (1)

M D
M D

Reputation: 47817

Try this way

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
android:focusable="true" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="60dip"
    android:layout_height="60dip"
    android:layout_marginTop="8dp"
    android:background="@drawable/logo"
    android:contentDescription="image"
    android:scaleType="center" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/icon"
    android:gravity="center"
    android:paddingLeft="3dp"
    android:paddingTop="10dp"
    android:shadowColor="#000000"
    android:shadowDx="1"
    android:shadowDy="1"
    android:shadowRadius="1.5"
    android:text="@string/app_name"
    android:textColor="#FFF" />

<TextView
    android:id="@+id/txt_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="-10dip"
    android:layout_toRightOf="@+id/icon"
    android:background="@drawable/badge_count2"
    android:contentDescription="badge"
    android:gravity="center"
    android:text="1"
    android:textColor="@color/White"
    android:textStyle="bold"
    android:visibility="visible" />

</RelativeLayout>

Create drawable/badge_count2.xml file like below

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="@color/red" >
</solid>

<stroke
    android:width="2dp"
    android:color="#FFFFFF" >
</stroke>

<padding
    android:bottom="2dp"
    android:left="7dp"
    android:right="7dp"
    android:top="3dp" />

<corners android:radius="10dp" >
</corners>

Output:

enter image description here

Upvotes: 4

Related Questions