Working with menu

critalsoft

Member
Licensed User
Longtime User
Hi Erel,

How can I add/remove, disable/enable and change the contents of the menu programatically.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Do you mean this:

Activity?

- Activity.AddMenuItem (Title As String, EventName As String)
for example in activity_create:

Activity.AddMenuItem("Start","mnuStart")
Activity.AddMenuItem("Options","mnuOptions")
end sub

B4X:
Sub mnuStart_click
msgbox("Start","")
end sub
Sub mnuOptions_click
msgbox("Start","")
end sub

- Activity.AddMenuItem2 (Title As String, EventName As String, Bitmap As android.graphics.Bitmap)

adds an image to the menu

- Activity.CloseMenu

Programmatically closes the menu.

- Activity.OpenMenu

Programmatically opens the menu.

XverhelstX
 
Upvote 0

critalsoft

Member
Licensed User
Longtime User
Thanks XverhelstX,

But this what I am not looking for. I need to add a menu item, enable or disable a menu item and change the label of the menu item programatically.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Thanks XverhelstX,

But this what I am not looking for. I need to add a menu item, enable or disable a menu item and change the label of the menu item programatically.

1) You should only add menu items in activity create.

2) the only way to enable/disable menu items is to maintain a flag for it so you can set/unset it. Then when the menu code is called, you can return from it without taking any action if the flag is unset.

3) You cannot change the menu item once it has been set.

Can I ask why you want to do this?

I ask because when I first started with B4A I wanted to change the menu items when I went to a new layout panel. After looking at it carefully I came to the conclusion that if it needed different menu items it really should be a separate activity.
 
Upvote 0

critalsoft

Member
Licensed User
Longtime User
Thanks kickaha,

I needed this, as I want to convert one of my Win Mobile Application into Android and in that I used to enable/disable and change the menu contents programatically.

I was also thinking the same which you just mentioned in your post.

Thanks again for your suggestions.
 
Upvote 0

boten

Active Member
Licensed User
Longtime User
....
3) You cannot change the menu item once it has been set.

Can I ask why you want to do this?

I ask because when I first started with B4A I wanted to change the menu items when I went to a new layout panel. After looking at it carefully I came to the conclusion that if it needed different menu items it really should be a separate activity.

True, but now I want one of the menu items to be a mute/unmute , so it would be nice if I could change the icon on the menu according to the sound's state.
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
I think I understand what you may want to do. It seems to have been much easier in VB. Since the menu has to be created (and recreated) whenever Activity_Create is called, the only way I can think of doing this is to some type of menu flag system, looking at the flags and creating each menu item as needed depending on the flag. For example, if you have only a mute button that you want to modify in this way, then it would look basically like this.

Sub Activity_Create
<Other stuff done in this sub>

'Create the the first menu items that are unchanging.
Activity.AddMenuItem2("Item_1", "Item1", <statement to load the bitmap>)
Activity.AddMenuItem2("Item_2", "Item1", <statement for this bitmap>)
'Create the changing mute button
If muteFlag = True then
Activity.AddMenuItem2("Unmute", "Mute", <statement to load 'muted' image>)
Else
Activity.AddMenuItem2("Mute", "Mute", <statement to load 'mute' image>)
End If
'Create other unchanging menu items.
<code to create these items>
End Sub

It may be possible to write some subs that do what is needed where by those subs are only called in Activity_Create. I don't know, I'd have to play with it.

The "Mute_Click" sub would mute or unmute the mic or speakers depending on the current state of muteFlag and then would set muteFlag appropriately. The user won't see any change until they tap the device's Menu button.

I don't see any other way of doing what you want except with something along these lines. I had to do a similar thing with an onscreen visual menu I created for a VB package I was part of producing.
 
Last edited:
Upvote 0

Shadow&Max

Active Member
Licensed User
Longtime User
I would like to do it because I have multiple uses for several screens... Panels pop up with different decisions than on the main screen. Right now, I have buttons across the bottom, and enable, disable, change text on the buttons as needed to correspond to what's happening at the time on the screen. In addition, I realized while testing my own app, that having buttons under the keyboard doesn't make things easier for anyone. I use one screen for several functions because it's less consumptive, uses calls to the database infrequently, and shares common lists between all of the functions. I think 5 buttons may be too much, and are really unnecessary. But three that could morph would be perfect...

I guess this was easy in VB because of the output device... the PC vs. a handheld.

I suppose there's no way to call Activity_Create programmatically...
 
Upvote 0
Top