Small problem with a code based on "ActionList"

alan1968

Active Member
Licensed User
Longtime User
Hello
I used as an example of erel (action list) to my menu to select elements in my application under B4A 1.8 it works correctly ,but 2.0 and 2.2 I do not have the selected line passing orange and my selection does not

the original code to work in 2.0 and 2.2

Here is the code to change for my

I do not see where the problem

I use the libraries Animation 1.02 and dialogs 2.70

thx! for help me

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Type ActionItem (Text As String, Image As Bitmap, Value As Object)
   Type ActionList (Pnl As Panel, Canvas As Canvas _
      ,Actions As List _
      ,Visible As Boolean _
      ,Title As String _
      ,InAnimation As Animation _
      ,OutAnimation As Animation _
      ,Selected As Int _
      ,ActivityName As String _
   )
   Dim fact As Float:fact=1
   Dim ItemHeight As Int : ItemHeight = 60*fact
   Dim TextSize As Float : TextSize = 20 'NOT dip.
   Dim TextLeftPos As Int : TextLeftPos = 70*fact
   Dim TextColor As Int : TextColor = Colors.White
   Dim TopPadding As Int : TopPadding = 5*fact
   Dim ImageWidth As Int : ImageWidth = 50*fact
   Dim ImageHeight As Int : ImageHeight = 50*fact
   Dim ImageLeftPos As Int : ImageLeftPos = 10*fact
   Dim ImageTopPos As Int : ImageTopPos = (ItemHeight - ImageHeight) / 2
   Dim SelectionColor As Int : SelectionColor = 0xFFFF8000
   ''
   Dim selection As Int
   Dim change As Boolean
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Dim al As ActionList
Dim scroll As ScrollView
Dim cd As CustomDialog2 
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
change=True
mnuitems
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub mnuitems'_click
If change= True Then
al =InitializeList(77%x, "SELECT ITEM + OK", "Main")
   AddActionItem("Bargraph", LoadBitmap(File.DirAssets, "mn_bargraph.png"), 17)
   AddActionItem("Bit label", LoadBitmap(File.DirAssets, "mn_label01.png"), 20)
   AddActionItem("Call page", LoadBitmap(File.DirAssets, "mn_page.png"), 14)
   AddActionItem("Cam ip", LoadBitmap(File.DirAssets, "mn_cam.png"), 18)
   AddActionItem("Classic button", LoadBitmap(File.DirAssets, "mn_bpn.png"), 2)
   AddActionItem("Fix label", LoadBitmap(File.DirAssets, "mn_label.png"), 0)
   AddActionItem("Flap", LoadBitmap(File.DirAssets, "mn_volet.png"), 8)
   AddActionItem("Galvanometer", LoadBitmap(File.DirAssets, "mn_galva.png"), 15)
   AddActionItem("Gauge", LoadBitmap(File.DirAssets, "mn_gauge.png"), 6)
   AddActionItem("Graph button", LoadBitmap(File.DirAssets, "bp_on.jpg"), 1)
   AddActionItem("Message", LoadBitmap(File.DirAssets, "mn_message.png"), 19)
   AddActionItem("On/off toggle button",LoadBitmap(File.DirAssets, "mn_tooglebp.png"), 3)
   AddActionItem("Oscilloscope", LoadBitmap(File.DirAssets, "mn_oscillo.png"), 16)
   AddActionItem("Picture", LoadBitmap(File.DirAssets, "mn_pic.png"), 10)
   AddActionItem("Receiver", LoadBitmap(File.DirAssets, "mn_recept.png"), 4)
   AddActionItem("Scenario", LoadBitmap(File.DirAssets, "mn_scenario.png"), 13)
   AddActionItem("Seekbar", LoadBitmap(File.DirAssets, "mn_seekbar.png"), 7)
   AddActionItem("Variable button", LoadBitmap(File.DirAssets, "mn_bpvar.png"), 11)
   AddActionItem("Variable label", LoadBitmap(File.DirAssets, "mn_labelvar.png"), 5)
   AddActionItem("Variable text", LoadBitmap(File.DirAssets, "mn_txtvar.png"), 9)
   AddActionItem("Web page", LoadBitmap(File.DirAssets, "mn_web.png"), 12)
   '
   Show
   
   Else
   ToastMessageShow("No edit mode !",False)
   End If
   End Sub
   Sub ActionList_Touch (Action As Int, X As Float, Y As Float) As Boolean
   'Delegates the touch event to the code module
   Touch(Sender, Action, X, Y)
   Return True
End Sub
Sub ActionList_Result (ReturnValue As Object)
   selection =ReturnValue
   End Sub 

Sub InitializeList (Width As Int, Title As String, ActivityName As String) As ActionList
   al.Initialize
   al.Actions.Initialize
   scroll.Initialize( (TopPadding + ItemHeight)*21)
   scroll.Panel.Height = (TopPadding + ItemHeight)*21
   Dim bmp As Bitmap
   bmp.InitializeMutable(Width, (TopPadding + ItemHeight)*21)
   al.Canvas.Initialize2(bmp)
   al.Pnl.Initialize("ActionList")
   al.Pnl.Tag = al
   Dim bd As BitmapDrawable
   bd.Initialize(bmp)
   bd.Gravity = Bit.Or(Gravity.LEFT, Gravity.TOP)
   al.Pnl.background = bd
   al.Title = Title
   al.InAnimation.InitializeAlpha("", 0, 1)
   al.InAnimation.Duration = 300
   al.OutAnimation.InitializeAlpha("", 1, 0)
   al.OutAnimation.Duration = 200
   al.ActivityName = ActivityName
   al.Selected=-1
   Return al
End Sub

Sub AddActionItem(Text As String, Image As Bitmap, ReturnValue As Object)
   Dim item As ActionItem
   item.Initialize
   item.Text = Text
   item.Image = Image
   item.Value = ReturnValue
   al.Actions.add(item)
End Sub

Sub Show 
Dim ret As Int
   'list_add =True
   cd.AddView(scroll,77%x, 60%y)
   If al.Visible Then 
      al.InAnimation.stop(al.Pnl)
      al.Pnl.RemoveView
   End If
   Dim h As Int
   h = (al.Actions.Size + 1) * ItemHeight + TopPadding
   scroll.Panel.AddView(al.Pnl, 0, 0, scroll.Width, (TopPadding + ItemHeight)*21)'
   If al.Visible = False Then
      al.InAnimation.Start(al.Pnl)
   End If
   Dim c As Canvas
   c = al.Canvas
   Dim r As Rect
   r.Initialize(0, 0, al.Pnl.Width, al.Pnl.Height)
   c.DrawRect(r, Colors.transparent, True, 0)
   c.DrawText(al.Title, scroll.Width / 2, TopPadding + ItemHeight / 2, Typeface.DEFAULT_BOLD, _
       TextSize, Colors.White, "CENTER")
   
   'Draw the items
   For i = 0 To al.Actions.Size - 1
      drawItem(i)
   Next
   al.Pnl.Invalidate
   al.Visible = True
    
ret = cd.Show("ADD NEW ITEMS", "OK", "CANCEL", "",LoadBitmapSample(File.DirAssets, "menu_add.png",25dip,25dip))
If ret=-1 Then
 '
Else


End If
End Sub


Sub Touch(Sdr As Object, Action As Int, X As Float, Y As Float)
   Dim pa As Panel
   pa = Sdr
   al = pa.Tag
   'Log(Action)
   
   If Action <> 1 Then 
      Dim currentItem As Int
      currentItem = Floor((Y - TopPadding - ItemHeight) / ItemHeight)
      If currentItem = al.Selected Then Return
      changeSelection(currentItem)
   Else If al.Selected >= 0 Then
      
      Dim item As ActionItem
      item = al.Actions.Get(al.Selected)
      
      
      CallSub2(al.ActivityName, "ActionList_Result", item.Value) 
      al.Selected =-1
      
   End If
End Sub
Sub drawItem(index As Int)
   Dim item As ActionItem
   item = al.Actions.Get(index)
   If item.Image.IsInitialized Then
      Dim imageRect As Rect
      imageRect.Initialize(ImageLeftPos, 0, ImageLeftPos + ImageWidth, 0)
      imageRect.Top = (index + 1) * ItemHeight + TopPadding + ImageTopPos
      imageRect.Bottom = imageRect.Top + ImageHeight
      al.Canvas.DrawBitmap(item.Image, Null, imageRect)
   End If
   
   al.Canvas.DrawText(item.Text, TextLeftPos, _
      (index + 1 + 0.5) * ItemHeight + TopPadding + al.Canvas.MeasureStringHeight(item.Text, Typeface.DEFAULT, TextSize) /2 _ 
         , Typeface.DEFAULT, TextSize, TextColor, "LEFT")
End Sub
Sub changeSelection(NewSelection As Int)
   Dim r As Rect
   Dim top As Int
   'delete the old selection
   If al.Selected >= 0 Then
      Dim pa As Path
      Dim y As Int
      y = (al.Selected + 1) * ItemHeight + TopPadding
      pa.Initialize(0,  y)
      pa.LineTo(0, y + ItemHeight)
      pa.LineTo(al.Pnl.Width, y + ItemHeight)
      pa.LineTo(al.Pnl.Width, y)
      al.Canvas.ClipPath(pa)
      r.Initialize(0, 0, al.Pnl.Width, al.Pnl.Height)
      al.Canvas.DrawRect(r, Colors.transparent, True, 0)
      drawItem(al.Selected)
      al.Canvas.RemoveClip
   End If
   If NewSelection >= 0 AND NewSelection < al.Actions.Size Then
      top = (NewSelection + 1) * ItemHeight + TopPadding + 3dip
      r.Initialize(3dip, top, al.Pnl.Width - 3dip, top + ItemHeight - 6dip)
      al.Canvas.DrawRect(r, SelectionColor, True, 0)
      drawItem(NewSelection)
      al.Selected = NewSelection
Else
      al.Selected = -1
   End If
   al.Pnl.Invalidate
End Sub
 

Attachments

  • v18.png
    v18.png
    89.1 KB · Views: 294
Last edited:

alan1968

Active Member
Licensed User
Longtime User
Please use File - Export as zip when uploading projects.

What is missing? The orange selection?

yes Erel, the orange selector not visible and not selection possible

i joint project in zip export (no picture in side) for picture in "files" foder upload zip attached at post before

thx!

alan
 

Attachments

  • menu_items.zip
    7.7 KB · Views: 187
Upvote 0
Top