Android Question Showing badge on actionbar item

Inman

Well-Known Member
Licensed User
Longtime User
I found this example with which we can have a shopping cart icon, with a badge indicating the number of items in the cart. While this works fine, I can see that the maximum value of badge is limited to 9 in code, probably because of limited space to display the value. But in my project the number of cart items will almost always be between 10 to 100 and in some cases above 100. How can the example be updated to fit at least a 2 digit number inside the badge circle?

SS-2017-06-08_17.06.06.png
 

Inman

Well-Known Member
Licensed User
Longtime User
Thanks Erel. I did try that. But then circle's corner got cut off and then trying to align the text exactly in the middle of the circle was proving difficult.

I will try again.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I basically doubled all the values in original example and tweaked the origin points and textsize a bit to adjust alignment and I believe now it can show 2 digit numbers properly

B4X:
Sub AddBadgeToIcon(bmp As Bitmap, Number As Int) As Bitmap
    Dim cvs As Canvas
    Dim mbmp As Bitmap
    mbmp.InitializeMutable(64dip, 64dip)
    cvs.Initialize2(mbmp)
    Dim target As Rect
    target.Initialize(0, 0, mbmp.Width, mbmp.Height)
    cvs.DrawBitmap(bmp, Null, target)
    If Number > 0 Then
        cvs.DrawCircle(mbmp.Width - 16dip, 16dip, 16dip, Colors.Red, True, 0)
        cvs.DrawText(Min(Number, 100), mbmp.Width - 17dip, 23dip, Typeface.DEFAULT_BOLD, 20, Colors.White, "CENTER")
    End If
    Return mbmp
End Sub
 
Upvote 0
Top