Android Question alter top of menu item on appcompat toolbar?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
See screenshot. The first item is a Fontawesome icon, 0xF1c0.
I would like to show it a bit higher, so in line with the further menu items.
Is this possible without manipulating a bitmap?

RBS
 

Attachments

  • menubar.png
    menubar.png
    48.5 KB · Views: 148

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Are you adding it with CSBuilder? You can change the vertical offset.
They are added as a bitmapdrawable:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    
    Dim oItem As ACMenuItem

    oItem = Menu.AddWithGroup2(4, i, i, arrMenu(i).strTitle1, arrMenu(i).bmdIconBitMapDrawable1)
    oItem.ShowAsAction = oItem.SHOW_AS_ACTION_ALWAYS

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
They are added as a bitmapdrawable:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
   
    Dim oItem As ACMenuItem

    oItem = Menu.AddWithGroup2(4, i, i, arrMenu(i).strTitle1, arrMenu(i).bmdIconBitMapDrawable1)
    oItem.ShowAsAction = oItem.SHOW_AS_ACTION_ALWAYS

RBS
This is the code that makes the bitmapdrawable in case the menu item is based on a Fontawesome or Material icon character (and not a file):

B4X:
Sub TextToBitmapDrawable(s As String, FontSize As Float, lFontColour As Int, _
                          iWidth As Int, iHeight As Int, _
                          TF As Typeface, strAlignment As String) As BitmapDrawable
 
    Dim bmp As Bitmap
    Dim cvs As Canvas
    Dim bmpd As BitmapDrawable
     
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    Dim h As Double = cvs.MeasureStringHeight(s, TF, FontSize)
    cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, TF, FontSize, lFontColour, strAlignment)
    bmpd.Initialize(bmp)
     
    Return bmpd
 
End Sub

RBS
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is how I add icons to the action bar:
B4X:
Dim cs As CSBuilder
cs.Initialize.Typeface(Typeface.FONTAWESOME).Size(30).VerticalAlign(5dip).Append(Chr(0xF17B)).PopAll
B4XPages.AddMenuItem(Me, cs).AddToBar = True
cs.Initialize.Typeface(Typeface.FONTAWESOME).Size(30).VerticalAlign(-10dip).Append(Chr(0xF17B)).PopAll
B4XPages.AddMenuItem(Me, cs).AddToBar = True

1637570178138.png
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This is how I add icons to the action bar:
B4X:
Dim cs As CSBuilder
cs.Initialize.Typeface(Typeface.FONTAWESOME).Size(30).VerticalAlign(5dip).Append(Chr(0xF17B)).PopAll
B4XPages.AddMenuItem(Me, cs).AddToBar = True
cs.Initialize.Typeface(Typeface.FONTAWESOME).Size(30).VerticalAlign(-10dip).Append(Chr(0xF17B)).PopAll
B4XPages.AddMenuItem(Me, cs).AddToBar = True

View attachment 122110
That looks nice coding, but I take it for this you will need B4XPages and unfortunately I haven't converted to this yet.
I guess I have to adjust the bitmap.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
OK, thanks, will try it and report back.

RBS
Of course the solution to this is very simple, just draw a bit higher on the canvas:

B4X:
Sub TextToBitmapDrawable(s As String, FontSize As Float, lFontColour As Int, _
                          iWidth As Int, iHeight As Int, _
                          TF As Typeface, strAlignment As String, _
                         iVerticalAdjust As Int, iHorizontalAdjust As Int) As BitmapDrawable
 
    Dim bmp As Bitmap
    Dim cvs As Canvas
    Dim bmpd As BitmapDrawable
     
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    Dim h As Double = cvs.MeasureStringHeight(s, TF, FontSize)
    cvs.DrawText(s, bmp.Width / 2 + iHorizontalAdjust, bmp.Height / 2 + h / 2 + iVerticalAdjust, _
                 TF, FontSize, lFontColour, strAlignment)
    bmpd.Initialize(bmp)
     
    Return bmpd
 
End Sub

RBS
 
Upvote 0
Top