B4A Library Popup Menu On Any View

Hi,
In continuation to my previous "ToggleButtons" Library, (https://www.b4x.com/android/forum/threads/my-toggle-button-library-rotation-issue-solved.91324/)
I am preparing multiple mini libraries for one of my bigger projects.

I though it would be good to share them with you all. If you use them, I would like to have your feedback.

This particular library will create a popup menu on click of any view in the activity. Needless to say you can have as many menus as you want and can call them upon click event.

Usage is simple:

Declare MenuOnAnyView object:

B4X:
    Dim movImgMenu As MenuOnAnyView

Initialize and add menu items:

B4X:
    movImgMenu.Initialize(Activity, Me, imgMenu, Colors.DarkGray, Colors.White, True, False)
    movImgMenu.AddMenuItem("Automobiles", "Autos")
    movImgMenu.AddMenuItem("Passenger Buses", "Autos")
    movImgMenu.AddMenuItem("Trucks", "Autos")

Capture the Click Event: Click Event will pass the "MenuName" as parameter.

B4X:
Sub Autos_Click(MenuName As String)
    Select MenuName
        Case "Automobiles"
            ToastMessageShow("Autos: Cars", False)
        Case "Passenger Buses"
            ToastMessageShow("Autos: Buses", False)
        Case "Trucks"
            ToastMessageShow("Autos: Trucks", False)
    End Select
End Sub

Other Methods are:
- MenuIsOpen - Returns Boolean
- OpenMenu
- CloseMenu
- DisableMenuItem
- EnableMenuItem

Tip:
If you have multiple MenuOnAnyView objects then you may want to close them all on click of another view. In my example if have created 5 different menu and on click of each I have call a sub to close them all (if open)

B4X:
Sub CloseAllMenus
    If movBtnMenu.MenuIsOpen == True Then movBtnMenu.CloseMenu
    If movImgMenu.MenuIsOpen = True Then movImgMenu.CloseMenu
    If movPmlAvatar.MenuIsOpen = True Then movPmlAvatar.CloseMenu
    If movLblMenu.MenuIsOpen = True Then movLblMenu.CloseMenu
    If movChkMenu.MenuIsOpen = True Then movChkMenu.CloseMenu
End Sub

Note:
I know that there might be many such libraries already available on the forum like this one or my other library of toggle buttons, but still wanted to create my own ... just for my learning purpose

Finally, I would also like to take this opportunity to thank all the Experts & other members here whom have written and shared so many pieces of code that has helped me so far and would continuously help me through out.


Cheers :)
 

Attachments

  • MenuOnAnyView_Example.zip
    120.2 KB · Views: 522
  • MenuOnAnyView_Lib_1.1.zip
    5.8 KB · Views: 463
  • Button.png
    Button.png
    70.4 KB · Views: 1,468
  • chkBox.png
    chkBox.png
    69.3 KB · Views: 1,411
  • Image.png
    Image.png
    71.4 KB · Views: 1,232
  • Label.png
    Label.png
    66.1 KB · Views: 1,178
  • Panel.png
    Panel.png
    72.1 KB · Views: 1,204
  • MenuOnAnyView_Example_1.2.zip
    120.3 KB · Views: 522
  • MenuOnAnyView_Lib_1.2.zip
    7.6 KB · Views: 532
  • MenuOnView_Lib_SurceCode.zip
    92.5 KB · Views: 595
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Thanks. I guess, that closing menu should be better safe, if to call without checking. With internal checking inside the lib, if needed.
 

peacemaker

Expert
Licensed User
Longtime User
Do you mean to say that the Lib should close the previously open menus?
No, i meant just errorless closing menu at any moment without checking if it's opened.

But your comment - is a good idea - it makes user to use one tap less: if user needs some menu it means that the previous menu is already not needed :)
 

msali

Member
Licensed User
Longtime User
How can there be no click effect?

Sorry for the late response. Actually, I only sit on my system twice a week (generally on Tuesday & Thursday).

Anyhow, I have added the click effect and have updated the library v1.2 Also have upadated the example to 1.2

Changes in v1.2:
  • Initialize method: added a "MenuClickColor As Int" parameter
  • Added a Boolean flag "ViberateOnMenuClick" that can be set immediately after the Initialize Method
B4X:
movImgMenu.Initialize(Activity, Me, imgMenu, Colors.DarkGray, Colors.White, Colors.LightGray, True, False)
movImgMenu.VibrateOnMenuClick= True ' By default it is set to False.



Hope this works for you :)
 

cxdzbl

Active Member
Licensed User
Sorry for the late response. Actually, I only sit on my system twice a week (generally on Tuesday & Thursday).

Anyhow, I have added the click effect and have updated the library v1.2 Also have upadated the example to 1.2

Changes in v1.2:
  • Initialize method: added a "MenuClickColor As Int" parameter
  • Added a Boolean flag "ViberateOnMenuClick" that can be set immediately after the Initialize Method
B4X:
movImgMenu.Initialize(Activity, Me, imgMenu, Colors.DarkGray, Colors.White, Colors.LightGray, True, False)
movImgMenu.VibrateOnMenuClick= True ' By default it is set to False.



Hope this works for you :)
thank you
 

Mahimahi

Member
Licensed User
Thank you for the wonderful popup menu on any view. I attempted to use your popup menu on a customListView where each row on the list is a panel with multiple controls, 1 checkbox, 1 button and 1 label. I have your popup menu initialized on the panel object for each customListView row, and to OpenMenu when user clicks on individual item on customListView.

Your popup menu works well when the list of items on customListView does not exceed the page length (or screen length). But I have some lists that has e.g. 40+ rows, which cannot all fit on screen, and scrolling is necessary before able to see those items. I noticed for those long lists, when user clicks on the customListView, no popup menu would show.

Please kindly advise. Thanks a lot.
 

msali

Member
Licensed User
Longtime User
With my new job i have not been able to continue my regular development.

If you want i can share full source code of my lib a you can modify it as per your will.

But only condition would be that you will have to share back the updated source code on this forum for all to take advantage.

Cheers :)
 

Mahimahi

Member
Licensed User
Thanks DonManfred. I am always concerned about the original library developer not receiving the message, thus posting on the same thread. I will post my question on a new thread next time.

Thanks msali for willing to share your full source code. I am sure everyone will benefit from your work on the library, including myself. I will most definitely share my modifications. Thanks a lot.
 

msali

Member
Licensed User
Longtime User
apologies for late response.
I have attached the zip file with Source Code "MenuOnView_Lib_SurceCode.zip" in the first thread
Zip file is not created B4A as this is my office PC and do not have it installed here.

Hope it works for you.
 
Hello msali, Is there any possibility to say the MenuOnView where to popup. I want to combine it with the flexible table from Klaus, but this object is a custom view, so i have to bind it to a dummy view instead.
 
Hello all together, I found a solution. In the flexible table you get the row-# and col-#. Together with the row-heigth and the col-width properties I could calculate a position of the MenuOnView. The only I had to do is to define a dummy view (e.g. a button), make it invisible and move it before opening the menu to the calulcated position.
Move Menu according choosen Cell in flexible Table.:
Sub Table1_CellLongClick(col As Int, row As Int)
    'Dummybutton defined in Layoutfile
    'In Sub Globals:
    'Dim Dummybutton AS Button in
    'Dim ColumnWidths() As Int = ( Array As Int(100dip, 250dip, 250dip, 250dip,240dip, 75dip, 75dip, 00, 00 ))
    Dim j As Int
    Dim xPos As Int
    Dim yPos As Int
        
    xPos = Table1.Left
    yPos = Table1.Top
    For j = 0 To col-1
        xPos = xPos + ColumnWidths(j)
    Next
    yPos = yPos + Table1.HeaderHeight + row*Table1.RowHeight
    movImgMenu.Initialize(Activity, Me, Dummybutton, Colors.DarkGray, Colors.White, Colors.Yellow, True, True)
    movImgMenu.AddMenuItem("Apples", "Fruits")
    movImgMenu.AddMenuItem("Bananas", "Fruits")
    movImgMenu.AddMenuItem("Oranges", "Fruits")
    Dummybutton.Left = xPos
    Dummybutton.Top = yPos
    movImgMenu.OpenMenu()
End Sub
 
Top