Android Question b4x menu item on bar with bitmap

JesseW

Active Member
Licensed User
Longtime User
using the youtube tutorial from AndroidDev (I believe his name is Milo), I was able to get a menuitem moved out from under the menu icon out to the screen, then set its bitmap property to an image. Exactly what I wanted. But when I tried to change it by issuing another bitmap assignment, it didn't change. Is it possible to get the menuitem bitmap to change on the fly, and if so, how?

thanks in advance. Here is the B4XMainPage code.
B4X Menu Icon Test:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=%PROJECT_NAME%.zip

'B4A ide://run?file=%WINDIR%\System32\cmd.exe&Args=/c&Args=start&Args=..\..\B4A\%PROJECT_NAME%.b4a
'B4i ide://run?file=%WINDIR%\System32\cmd.exe&Args=/c&Args=start&Args=..\..\B4i\%PROJECT_NAME%.b4i
'B4J ide://run?file=%WINDIR%\System32\cmd.exe&Args=/c&Args=start&Args=..\..\B4J\%PROJECT_NAME%.b4j

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private mi As B4AMenuItem
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    mi = B4XPages.AddMenuItem(Me, "Change Number")
    mi.AddToBar = True
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "1.png", 64dip, 64dip, False)
    End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "2.png", 64dip, 64dip, False)
End Sub

Edit: I'm thinking a little background might help, at least to as to the why I want to be able to change it. I'm working on a project for bulk feed drivers to help them find farms they're delivering to. The mileage pay is different if you've been there 1 year or less, between 1-2 years, etc... basically, there's pay years 1, 2, 3, 4 and 5+. so the app correctly displays the current menu icon so they can see their pay year at a glance. It's the only menu item, so it just looks better than the menu icon with a menu item after it's clicked. I then set up a b4xlisttemplate dialog to allow a user new to the app to select their pay year, but it didn't change the icon. Not right then. It would correct itself after they closed the app and reopened it. I'd really like it to change right after they change their pay year tho... much more professional looking - thanks
 

Attachments

  • menuicontest.zip
    74.1 KB · Views: 141
Last edited:

TILogistic

Expert
Licensed User
Longtime User
use:

Change:

B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    Dim MenuItemsAll As List = B4XPages.GetManager.GetPageInfoFromRoot(Root).Parent.MenuItems
    MenuItemsAll.Clear
   
    mi = B4XPages.AddMenuItem(Me, "Change Number")
    mi.AddToBar = True
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "1.png", 64dip, 64dip, False)
   
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("invalidateOptionsMenu", Null)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    Dim MenuItemsAll As List = B4XPages.GetManager.GetPageInfoFromRoot(Root).Parent.MenuItems
    MenuItemsAll.Clear
   
    mi = B4XPages.AddMenuItem(Me, "Change Number")
    mi.AddToBar = True
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "2.png", 64dip, 64dip, False)
   
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("invalidateOptionsMenu", Null)
End Sub
 

Attachments

  • 1.gif
    1.gif
    72.5 KB · Views: 167
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
thank you! I will look at this shortly.

just at a glance, I'm wondering if setting the new bitmap to the existing mi object without clearing the options list and adding it again, and invalidating with the jo object would get the desired result? I'm gonna try. thx again!

Edit: it does work! this works great!
changes:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=%PROJECT_NAME%.zip

'B4A ide://run?file=%WINDIR%\System32\cmd.exe&Args=/c&Args=start&Args=..\..\B4A\%PROJECT_NAME%.b4a
'B4i ide://run?file=%WINDIR%\System32\cmd.exe&Args=/c&Args=start&Args=..\..\B4i\%PROJECT_NAME%.b4i
'B4J ide://run?file=%WINDIR%\System32\cmd.exe&Args=/c&Args=start&Args=..\..\B4J\%PROJECT_NAME%.b4j

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private mi As B4AMenuItem
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    mi = B4XPages.AddMenuItem(Me, "Change Number")
    mi.AddToBar = True
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "1.png", 64dip, 64dip, False)
    End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "2.png", 64dip, 64dip, False)
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("invalidateOptionsMenu", Null)
End Sub
 
Upvote 0
Top