B4A Class [B4X] [XUI] AS Popup Menu

This is a simple cross platform Popup Menu.

I spend a lot of time in creating views, like this and to create a high quality view cost a lot of time. If you want to support me and further views, then you can do it here by Paypal or with a coffee. :)

Left: B4J Right: B4A

1598981805790.png
Screenshot_20200901-193834.jpg
ezgif.com-resize.gif
Screenshot_20201124-140900.jpg
IMG_2331.jpg
KTa7gKtcmrbG6pEHsg6rdVqCE1ChquLRGgVWzvyhxJWWJFU0wI.jpeg

(54) mage.png

Fast example:
aspm_MoreOptions.Initialize(Root,Me,"aspm_MoreOptions")
 
aspm_MoreOptions.ItemLabelProperties.ItemBackgroundColor = xui.Color_ARGB(255,19, 20, 22) 'Change the Background Color of the item
aspm_MoreOptions.AddMenuItem("Share location",1)'Add item
aspm_MoreOptions.AddMenuItem("Settings",2)'Add item

aspm_MoreOptions.OrientationHorizontal = aspm_MoreOptions.OrientationHorizontal_RIGHT
aspm_MoreOptions.OrientationVertical = aspm_MoreOptions.OrientationVertical_BOTTOM
 
aspm_MoreOptions.ItemLabelProperties.BackgroundColor = xui.Color_ARGB(200,0,0,0)'black
 
aspm_MoreOptions.OpenMenu(xlbl_MoreOptions,150dip)
ASPopupMenu
Author: Alexander Stolte
Version: 1.08

  • ASPM_ItemLabelProperties
    • Fields:
      • BackgroundColor As Int
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • ItemBackgroundColor As Int
      • LeftRightPadding As Float
      • TextAlignment_Horizontal As String
      • TextAlignment_Vertical As String
      • TextColor As Int
      • xFont As B4XFont
    • Functions:
      • Initialize
        Initializes the fields to their default value.
  • ASPM_TitleLabelProperties
    • Fields:
      • BackgroundColor As Int
      • Height As Float
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • ItemBackgroundColor As Int
      • LeftRightPadding As Float
      • TextAlignment_Horizontal As String
      • TextAlignment_Vertical As String
      • TextColor As Int
      • xFont As B4XFont
    • Functions:
      • Initialize
        Initializes the fields to their default value.
  • ASPopupMenu
    • Events:
      • ItemClicked (Index As Int, Tag As Object)
    • Fields:
      • AutoHideMs As Int
      • CloseDurationMs As Int
      • OpenDurationMs As Int
    • Functions:
      • AddMenuItem (text As String, tag As Object) As String
      • AddTitle (Text As String, height As Float) As String
      • Class_Globals As String
      • Clear As String
        clears the list
      • CloseMenu As String
      • CreateASPM_ItemLabelProperties (TextColor As Int, xFont As B4XFont, TextAlignment_Vertical As String, TextAlignment_Horizontal As String, BackgroundColor As Int, ItemBackgroundColor As Int, LeftRightPadding As Float) As ASPM_ItemLabelProperties
      • CreateASPM_TitleLabelProperties (TextColor As Int, xFont As B4XFont, TextAlignment_Vertical As String, TextAlignment_Horizontal As String, BackgroundColor As Int, ItemBackgroundColor As Int, LeftRightPadding As Float, Height As Float) As ASPM_TitleLabelProperties
      • getBase As B4XView
      • getisOpen As Boolean
        checks if the menu is open
      • getItemLabelProperties As ASPM_ItemLabelProperties
        change the label properties, call it before you add items
        <code>ASScrollingTags1.LabelProperties.xFont = xui.CreateDefaultBoldFont(15)</code>
      • GetMenuItemAt_Background (index As Int) As B4XView
        gets a menu item background (parent of label)
      • GetMenuItemAt_Label (index As Int) As B4XView
        gets a menu item label - the label with the text
      • getOrientationHorizontal_LEFT As String
      • getOrientationHorizontal_MIDDLE As String
      • getOrientationHorizontal_RIGHT As String
      • getOrientationVertical_BOTTOM As String
      • getOrientationVertical_TOP As String
        Vertical = Top,Bottom
        Horizontal = Left,Middle,Right
      • getSize As Int
        gets the list size
      • getTitleLabelProperties As ASPM_TitleLabelProperties
        change the label properties, call it before you add the title
        <code>ASScrollingTags1.TitleLabelProperties.xFont = xui.CreateDefaultBoldFont(20)</code>
      • Initialize (Parent As B4XView, CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • OpenMenu (view As B4XView, width As Float)
        Opens the menu on a view
      • OpenMenu2 (parent As B4XView, width As Float)
        Open the menu on center of parent/view
      • RemoveTitle As String
        Removes the title if exists
      • Resize (parent_width As Float, parent_height As Float) As String
      • setActivityHasActionBar (value As Boolean) As String
      • setCloseAfterItemClick (enabled As Boolean) As String
        closes the menu automatically after clicking on an item
        standard is True
      • setDividerColor (color As Int) As String
        the color of the dividers
      • setDividerEnabled (enable As Boolean) As String
        Adds or Removes the dividers
      • setDividerHeight (height As Float) As String
        the height of the dividers
      • setIsInSpecialContainer (value As Boolean) As String
        set it true if the target is on a listview or as a child on a panel where the left and top values differ from the form
      • setItemBackgroundColor (crl As Int) As String
        sets the item background color for all items
      • setMenuCornerRadius (radius As Int) As String
      • setOrientationHorizontal (orientation As String) As String
      • setOrientationVertical (orientation As String) As String
      • ViewScreenPosition (view As B4XView) As Int()
    • Properties:
      • ActivityHasActionBar
      • Base As B4XView [read only]
      • CloseAfterItemClick
        closes the menu automatically after clicking on an item
        standard is True
      • DividerColor
        the color of the dividers
      • DividerEnabled
        Adds or Removes the dividers
      • DividerHeight
        the height of the dividers
      • IsInSpecialContainer
        set it true if the target is on a listview or as a child on a panel where the left and top values differ from the form
      • isOpen As Boolean [read only]
        checks if the menu is open
      • ItemBackgroundColor
        sets the item background color for all items
      • ItemLabelProperties As ASPM_ItemLabelProperties [read only]
        change the label properties, call it before you add items
        <code>ASScrollingTags1.LabelProperties.xFont = xui.CreateDefaultBoldFont(15)</code>
      • MenuCornerRadius
      • OrientationHorizontal
      • OrientationHorizontal_LEFT As String [read only]
      • OrientationHorizontal_MIDDLE As String [read only]
      • OrientationHorizontal_RIGHT As String [read only]
      • OrientationVertical
      • OrientationVertical_BOTTOM As String [read only]
      • OrientationVertical_TOP As String [read only]
        Vertical = Top,Bottom
        Horizontal = Left,Middle,Right
      • Size As Int [read only]
        gets the list size
      • TitleLabelProperties As ASPM_TitleLabelProperties [read only]
        change the label properties, call it before you add the title
        <code>ASScrollingTags1.TitleLabelProperties.xFont = xui.CreateDefaultBoldFont(20)</code>
Changelog
  • 1.00
    • Release
  • 1.01
    • Adds background_color global variable - the background color of the parent view during the menu is open
    • Adds setItemBackgroundColor - change the items background color
    • Better Handling in "Nested Layouts"
    • Add ActivityHasActionBar - set to true if you have a ActionBar enabled
    • Add IsInSpecialContainer - set it true if the target is on a listview or as a child on a panel where the left and top values differ from the form
    • Add setOrientationVertical
    • Add setOrientationHorizontal
    • Adds "Enums" for Orientations -TOP,BOTTOM -LEFT,MIDDLE,RIGHT
  • 1.02
    • Add OpenMenu2 - Opens the menu in center of the parent view
    • Add Clear - clear all
    • Add getBase - gets the base view to customize it
    • Add getLabelProperties - customize it if you want, before you add a new menu item
      • Screenshot_20201124-134749.jpg
      • this is now possible
        • aspm_main.LabelProperties.ItemBackgroundColor = xui.Color_ARGB(255,Rnd(1,256), Rnd(1,256), Rnd(1,256))
    • Remove background_color and itembackground_color
      • Added to getLabelProperties
    • Add GetMenuItemAt_Label
    • Add GetMenuItemAt_Background
    • Add getSize
    • Add setMenuCornerRadius - sets the corner radius for the menu (works only in B4A and B4I)
      • Screenshot_20201124-140900.jpg
  • 1.03
    • Add LeftRightPadding to ASPM_LabelProperties - label padding to have more space left and right for example HorizontalCenter = LEFT
    • OpenMenu the background is now smoother on show
    • setMenuCornerRadius is now available on B4J (thanks @Erel)
  • 1.04
    • getLabelProperties is renamed to getItemLabelProperties
    • Add AddTitle - you can now add a title to the menu
      • Add TitleLabelProperties - you can change the title properties with it
    • Add RemoveTitle - Removes the title if exists
    • Add AutoHideMs
    • Add DividerEnabled - you can now show dividers between the items
      • Add DividerHeight - the height of the dividers
      • Add DividerColor - the color of the dividers
  • 1.05
    • OpenMenu Show Animation with the background is now animated
    • BugFixes
  • 1.06
    • BugFixes
  • 1.07
    • Add get isOpen - checks if the menu is open
    • BugFix - if you called OpenMenu several times without closing the menu, then the menu was added several times
    • Add set CloseAfterItemClick - closes the menu automatically after clicking on an item
      • standard is True
    • Add set and get CloseDurationMs - the duration it takes for the menu to be completely closed
    • Add set and get OpenDurationMs - the duration it takes for the menu to be fully visible
  • 1.08
    • Intern Function IIF renamed to iif2
  • 1.09
    • Add set MenuViewGap - sets the gap between the menu and the attached view
    • Add get TriangleProperties
    • Add set ShowTriangle - only visible if you open the menu with OpenMenu
      • Default: False
  • 1.10
    • BugFix
  • 1.11
    • Add OpenMenuAdvanced - You can set the Left, Top and Width value to show the menu on the parent
    • Add MenuHeight - gets the MenuHeight even if the menu is not yet visible
    • BugFixes
  • 1.12 (read more)
    • Add get and set ClickColor
    • Add get and set BackgroundPanelColor
    • B4J Only - Hover Over Item with color
  • 1.13 (read more)
    • Add AddMenuItemWithIcon
    • Add get IconProperties
    • Add get and set ItemHeight
Have Fun :)
 

Attachments

  • AS PopupMenu Example.zip
    10.5 KB · Views: 858
  • ASPopupMenu.b4xlib
    7.1 KB · Views: 157
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
aspm_main.TitleLabelProperties.BackgroundColor = xui.Color_Gray
is now fixed.
this color is automatically also set on the background at the title.
aspm_main.TitleLabelProperties.ItemBackgroundColor = xui.Color_Gray
is actually not needed, I think I will remove it soon
 

Daica

Active Member
Licensed User
I'm trying to use this popup menu together with your tab menu but I'm getting an error about type mismatch.

B4X:
aspm.Initialize(B4XTable1, Me, "tbl_menu")

If I try to use the Activity as the parent, the app crash with the following error:
B4X:
aspm.Initialize(Me, Me, "tbl_menu")
aspm.OpenMenu2(Me, 100dip)

Error occurred on line: 87 (ASPopupMenu)
java.lang.ClassCastException: java.lang.Class cannot be cast to android.view.View
at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:89)
at anywheresoftware.b4a.objects.B4XViewWrapper.getWidth(B4XViewWrapper.java:142)
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
Under the hood you need a panel.
Because i add my base panel to the parent panel.

I dont know about the B4XTable1, but it should be something like this: B4XTable1.base or B4XTable1.mBase
 

Daica

Active Member
Licensed User
I added a transparent panel onto my layout and used that as the parent. It works, but the popup is cut off. I think I might have to add a transparent button on that panel so the popup doesn't get cut off, unless I can position the popup somehow?
 

Alexander Stolte

Expert
Licensed User
Longtime User
This is because your parent is not big enough. Either you set a bigger panel, or you set the activity/root panel, then you are on the safe side.
On "OpenMenu" you set the view where you want to see the popup.
 

Daica

Active Member
Licensed User
Yeah I had to use a transparent panel as the parent, and then put another transparent view on that panel where I want to show the menu. It works this way.

For some reason, the last item on the menu cannot be clicked on though. Do you know what might be causing that?
EDIT:
Using your example project, it works fine. I guess it must be something I am doing with my panel
 
Last edited:

Daica

Active Member
Licensed User
In B4I and B4J, your transparent panel may intercept the events. On B4A it can only be if there is a view above the popup menu which also listens to click events.

I think it may have been my transparent button that is taking the click event away from the last item then. I think I will just create a button that is 1x1 in dimension through code and place it on my transparent panel, then set the menu to open where the 1x1 button is so its not big enough to take away the click
 

Noelkman

Member
Licensed User
Longtime User
Shouldn't the menu close automatically on Item Select? Actually it does not.
If I don't call [...].CloseMenu the menu stays open. If you then call [...].OpenMenu() again the parent view gets darker and darker until black out.
I used the example from the first post.
 

Alexander Stolte

Expert
Licensed User
Longtime User
Shouldn't the menu close automatically on Item Select?
I've always wondered about that, too. Are there usecases where this cannot be the case?
I'll write it down and add it to the next update.
If you then call [...].OpenMenu() again the parent view gets darker and darker until black out.
I'll write it down and fix it with the next update.
 

Daica

Active Member
Licensed User
I've always wondered about that, too. Are there usecases where this cannot be the case?
I'll write it down and add it to the next update.

I'll write it down and fix it with the next update.

Also, similar to your DraggableBottomCard, adding IsOpen as Boolean would be very helpful :)
Maybe instead of making the menu close automatically after clicking the item, make it an option like Menu.CloseAfterItemClick As Boolean and set it to true. Anyone who do not want the menu to close can set it to false
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
Update
1.07

  • Add get isOpen - checks if the menu is open
  • BugFix - if you called OpenMenu several times without closing the menu, then the menu was added several times
  • Add set CloseAfterItemClick - closes the menu automatically after clicking on an item
    • standard is True
  • Add set and get CloseDurationMs - the duration it takes for the menu to be completely closed
  • Add set and get OpenDurationMs - the duration it takes for the menu to be fully visible
 

Kope

Active Member
Licensed User
Hi, thanks for this library

am currently not using b4x pages and it kinda hard using this library especially this line
aspm_main.Initialize(Root,Me,"aspm_main")

How can the aspopupmenu be initiatize?

Thank You
 
Top