Android Question updating menu items

Marco Nissen

Active Member
Licensed User
Longtime User
Hi

if I added a menu using AddMenu and also included images, is there any way to update the image later on?

The reason is, I display a mail envelope to allow the user to access the mailbox.
If there is a new incoming email which is unread, the image should change to indicate this.

I found something for Android in general, but not for B4A
Sample discussion is here: http://stackoverflow.com/questions/11006749/change-icons-in-actionbar-dynamically

Thank you
Marco
 

Marco Nissen

Active Member
Licensed User
Longtime User
Yes, but Erel: can you suggest a way to support this? Like enumerating the existing menu items for an Activity and then through direct API access modifying an image or text? e.g. gmail now uses this for notification of unread items
 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
It is not possible to change the menu icons. You can add a custom view to the action bar: http://www.b4x.com/android/forum/th...er-actionbar-library.36786/page-3#post-226174

You can add a Panel and then add or remove items from this panel.

Just tried it out. It sort of works, but I have to add empty buttons to the menu, just to have enough room to place the customized bar.
and the events for the customized panel are not trigged, but the events for the empty buttons
replacing the picture works fine, so there might be some internal confusing about which events should be triggered

Does that make any sense?

B4X:
Activity.AddMenuItem3("", "btnmail", LoadBitmap(File.DirAssets, "empty.png"), True)
Activity.AddMenuItem3("", "btnmenu", LoadBitmap(File.DirAssets, "empty.png"), True)
Dim jo As JavaObject = bar
dim alternateMenu As Panel
alternateMenu.Initialize("altMenu")
alternateMenu.LoadLayout("alternateMenuMain")
jo = jo.GetField("bar")
jo.RunMethod("setDisplayShowCustomEnabled", Array As Object(True))
jo.RunMethod("setCustomView", Array As Object(alternateMenu))
btnMail.SetBackgroundImage(LoadBitmap(File.DirAssets,"730-envelope-selected.png"))

Sub btnmail_click
    StartActivity(MessagesList)
End Sub
 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
I added this

B4X:
alternateMenu.Width=100dip

But I get this exception.

B4X:
(Main, 87) Panel.LoadLayout should only be called after the panel was added to its parent. (warning #1001)
java.lang.NullPointerException

    at anywheresoftware.b4a.objects.ViewWrapper.setWidth(ViewWrapper.java:124)
    at com.wurzelkraut.Stash2Go.main._activity_create(main.java:809)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at com.wurzelkraut.Stash2Go.main.afterFirstLayout(main.java:98)
    at com.wurzelkraut.Stash2Go.main.access$100(main.java:16)
    at com.wurzelkraut.Stash2Go.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
sending message to waiting queue of uninitialized activity (submitjob)
 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
Try to add a Panel to this panel and then set the width of the second panel.

just moved it after the call "setcustomview", then it worked without an exception.
however, the buttons are then not aligned to the right side, but appear right after the title (i.e. somewhere in the middle of the bar)

I guess I leave it with the above workaround, but it does not feel right
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…