Android Question Hamburguer menu in the B4xMainPage

lumbanico

Member
Hi, there.

I can show a hamburguer menu in a B4xPage, but I don't know haw to do it in the B4xMainPage.

I have tried it copying the next code in the Initialize sub and in the B4XPage_Created sub, but in both cases only show the icon in the pages, not in the B4XMainPage:

B4X:
HamburgerIcon = xui.LoadBitmapResize(File.DirAssets, "my.png", 32dip, 32dip, True)
B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
    Dim bd As BitmapDrawable
    bd.Initialize(HamburgerIcon)
    B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))

Please, help.

Thank in advance.
 

toby

Well-Known Member
Licensed User
Longtime User
For the menu icon to appear on B4XMainPage or any page, you need to create the menu options.
B4X:
dim lstItems as list
lstItems.Initialize
lstItems.AddAll(Array As String("Option 1", "Option 2"))


Private Sub UpdateMenuItems(lstItems As List, iconFile As String)
'    Dim bmp As B4XBitmap = CreateIconWithBadge(CartBitmap, BadgeNumber)
    '1 clear existing menu items, if any
    Dim menus As List = B4XPages.GetManager.GetPageInfoFromRoot(Root).Parent.MenuItems
    menus.Clear
    
    'add menu items
    '2. add menu items
    Dim mi As B4AMenuItem' = B4XPages.AddMenuItem(Me, "cart")
    For i =0 To lstItems.Size-1
        Dim item As String =lstItems.Get(i)
        mi=B4XPages.AddMenuItem(Me, item)
        'add icon to first item:
        If i=0 And iconFile.Length>0 And File.Exists(File.DirAssets, iconFile) Then
            Dim bmp As Bitmap
            bmp.Initialize(File.DirAssets, iconFile)
            mi.AddToBar = True
            mi.Bitmap = bmp
        End If
    Next
    
    '3 force refresh
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    ctxt.RunMethod("invalidateOptionsMenu", Null)
End Sub
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Here is a simple example
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    B4XPages.AddMenuItem(Me, "Option 1")
    B4XPages.AddMenuItem(Me, "Option 2")
    
End Sub
 
Upvote 0
Top