Android Question How to add icon to acmenu?

dragonguy

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear
    Menu.Add(0, 0, Chr(0xE8B8) & " " & "Setting", Null)
    Menu.Add(1, 0, Chr(0xE8B8) & " " &"Export/Import", Null)
    Menu.Add(2, 0, Chr(0xE88F) & " " &"About", Null)
    Menu.Add(3, 0, Chr(0xE879) & " " & "Exit", Null)
    
End Sub

or

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear
    Menu.Add(0, 0,"Setting", FontToBitmap(Chr(0xE8B8),True,14,Colors.White))
    Menu.Add(1, 0, "Export/Import", FontToBitmap(Chr(0xE8B8),True,14,Colors.White))
    Menu.Add(2, 0,"About", FontToBitmap(Chr(0xE88F),True,14,Colors.White))
End Sub

Public Sub FontToBitmap (text As String, IsMaterialIcons As Boolean, FontSize As Float, color As Int) As B4XBitmap
    Dim xui As XUI
    Dim p As Panel = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 32dip, 32dip)
    Dim cvs1 As B4XCanvas
    cvs1.Initialize(p)
    Dim t As Typeface
    If IsMaterialIcons Then t = Typeface.MATERIALICONS Else t = Typeface.FONTAWESOME
    Dim fnt As B4XFont = xui.CreateFont(t, FontSize)
    Dim r As B4XRect = cvs1.MeasureText(text, fnt)
    Dim BaseLine As Int = cvs1.TargetRect.CenterY - r.Height / 2 - r.Top
    cvs1.DrawText(text, cvs1.TargetRect.CenterX, BaseLine, fnt, color, "CENTER")
    Dim b As B4XBitmap = cvs1.CreateBitmap
    cvs1.Release
    Return b
End Sub

the menu didn't show up the the icon.
How to add icon to acmenu?
 

Attachments

  • nQSM0.png
    nQSM0.png
    24.8 KB · Views: 480

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
That screenshot just a sample. I want add like that sample, but the code didn't work. Just show the text only.
I will try later. Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2018-08-05_10.43.43.png


Regular menu:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cs As CSBuilder
   Dim b As Bitmap = LoadBitmapResize(File.DirAssets, "baseline_settings_phone_black_48dp.png", 32dip, 32dip, True)
   cs.Initialize.Image(b, 32dip, 32dip, False).Append("         ").Append("Test").PopAll
   Activity.AddMenuItem3(cs, "menu", Null, False)
   Activity.AddMenuItem3(cs, "menu", Null, False)
   Activity.AddMenuItem3(cs, "menu", Null, False)
   Activity.AddMenuItem3(cs, "menu", Null, False)
End Sub
ACMenuItem also accepts a CharSequence so it should work.
 
Upvote 0
Top