The background setting in TextView is small because I can’t put the text in relation to the icon.
In fact, you can put your text relative to your icon. You need to separate the basket icon and the icon of the number icon as separate images and lay them out separately. I had to do it myself not so long ago, and I did it using RelativeLayout with ImageView basket icon and TextView for numbers with 9-patch icon badge as background.
The trick is to align your TextView number to the left and bottom of the ImageView Recycle Bin icon, and then use strong> and bottom left to put your icon icon on the top and right of the Recycle Bin. Thus, the number icon is always snapped based on the basket icon.
Also, set the gravity your TextView to center , as the numbers grow wider, the relative position of the text is about the same. Finally, use padding on your TextView to control how much difference there is between the edge of the number and the edge of the 9-patch “badge”.
Here is a snippet of my implementation (I partially edited):
<RelativeLayout android:id="@+id/cartButton" android:layout_width="wrap_content" android:layout_height="fill_parent" > <ImageView android:id="@+id/cartIconImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@drawable/ic_menu_cart" /> <TextView android:id="@+id/cartBadge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/cartIconImageView" android:layout_alignLeft="@+id/cartIconImageView" android:layout_marginBottom="10dp" android:layout_marginLeft="9dp" android:background="@drawable/state_list_cart_badge" android:gravity="center" android:includeFontPadding="false" android:lines="1" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="7" /> </RelativeLayout>
And here is what it looks like:


source share