Android Question Button Click with passing parameters

ALBRECHT

Active Member
Licensed User
Hello,

I have a sub that fill a custom view with a product list.

The _ItemClick event is handling perfectly his job , when we are clicking into the global item line, but :

that product list (code below) have 2 kind of added buttons which have to launch (with button_Click) 2 differents script than the global Itemclick :

- BtnVa for "ViewArticles" event
- and BtnVc for "ViewCat" event

How to send some parameters with that buttons initialisation or via an other way ?

B4X:
Sub Product_List(Wit As Int, Hei As Int, Lbl0 As String, NbArt As Int, NbCatFils As Int, NumImg As String, BtnMenu As Button) As Panel
    Dim p As Panel
    p.Initialize("")
 
    Dim ImgUrl As String
    Dim links As Map
    links.Initialize
    If NumImg <> Null And NumImg <> "" Then
        ImgUrl = Main.ConnectWebSite & Main.PathImg1 & NumImg & Main.ExtImg1
        Dim ImgArticle As ImageView
        ImgArticle.Initialize("ViewCat")
        links.Put(ImgArticle, ImgUrl)
        CallSubDelayed2(ImageDownloader, "Download", links)
        ImgArticle.Width = 80dip
        ImgArticle.Height = 60dip
        ImgArticle.Gravity = Gravity.FILL
        p.AddView(ImgArticle, 0, 0, 80dip, 60dip) 'Image
    End If
 
    Dim Lbl As Label
    Lbl.Initialize("ViewCat")
    Lbl.Text = Lbl0
    Lbl.TextSize = 16
    Lbl.Gravity = Gravity.LEFT + Gravity.CENTER_VERTICAL
    p.AddView(Lbl, 85dip, 0, (Wit - 100) * 1dip, Hei) 'Libellé

    Dim BtnVa As Button '<**************************
    BtnVa.Initialize("ViewArticles")
    If NbArt > 0 Then
        BtnVa.Typeface = BtnMenu.Typeface
        BtnVa.Text = Chr(0xF06E) & NbArt
        BtnVa.TextSize = 16
        BtnVa.Width = 45dip
        BtnVa.Height = 45dip
        BtnVa.Color = Colors.Transparent
        BtnVa.TextColor = Colors.Black
        If NbCatFils > 0 Then
            p.AddView(BtnVa, Wit - 90dip, 0, 45dip, Hei - 2dip) 'Button View Articles
        Else
            p.AddView(BtnVa, Wit - 45dip, 0, 45dip, Hei - 2dip) 'Button View Articles
        End If
    End If
 
    Dim BtnVc As Button '<***************************
    BtnVc.Initialize("ViewCat")
    If NbCatFils > 0 Then
        BtnVc.Typeface = BtnMenu.Typeface
        BtnVc.Text = Chr(0xF101)
        BtnVc.TextSize = 18
        BtnVc.Width = 45dip
        BtnVc.Height = 45dip
        BtnVc.Color = Colors.Transparent
        BtnVc.TextColor = Colors.Black
        p.AddView(BtnVc, Wit - 45dip, 0, 45dip, Hei - 2dip)
    End If
 
    Dim Line As Panel
    Line.Initialize("")
    Line.Height = 2dip
    Line.Color = Colors.White
    Line.Top = Hei - Line.Height
    p.AddView(Line, 0, Line.Top, Wit, Line.Height) 'line
 
    Return p
End Sub
Many thanks
 
Last edited:

emexes

Well-Known Member
Licensed User
I don't fully understand the question (problem probably on my side of the fence ;-) but I feel like the answer might be to use the tag field of the button(s) which you can access like:
B4X:
Sub AnyButton_Click

    Dim V As View = Sender
    Dim T As String = V.Tag

    EqualsSignAt = T.IndexOf("=")
    If EqualsSign <> -1 Then
        Dim TagAction As String = T.SubString2(0, EqualsSignAt).Trim
        Dim TagParameter As String = T.SubString(EqualsSignAt + 1).Trim

        'eg Tag is "Article=288" then TagAction is "Article" and TagParameter is "288"

        CallSub2(Main, "View_" & TagAction, TagParameter)    'example
    End If

End Sub
or:
B4X:
Sub AnyButton_Click

    Dim V As View = Sender
    Dim T As String = V.Tag

    Select Case T.Trim.ToUpperCase    'trust but verify :-)
        Case "VIEWARTICLES"
            'view article

        Case "VIEWCAT"
            'view catalog

        Case Else
            'view item

    End Select

End Sub
edit: fixed Sender (was Me)
 
Last edited:

ALBRECHT

Active Member
Licensed User
Thanks emexes,
If i understand correctly the Idea :
As there is no button event which carry some charges like "_onclick(values as string)" then we can use the TAG property to set some paramètres on the sender side and get it at the onclick évent. Great
 

emexes

Well-Known Member
Licensed User
If i understand correctly the Idea :
As there is no button event which carry some charges like "_onclick(values as string)" then we can use the TAG property to set some parameters on the sender side and get it at the onclick event. Great
If "the sender side" is "a specific button" then yes. The tag of a button can be used like a parameter to the _Click event.

This makes sense more when a single _Click handler is being used by multiple buttons... but you can change a button's tag programmatically, so it can sometimes also be useful even when the _Click handler *knows* which button called it.

I use the tags for storing information about how to tweak views to fit different layout screen sizes, eg for text scaling, where some labels are constant-length headings where you want the text to scale ~1:1 with the screen, but other labels are a window into a long text where you want the text to scale up at a slower rate so that more of it is visible without scrolling.
 

ALBRECHT

Active Member
Licensed User
It works better with sender than Me, like :

B4X:
Sub ViewCat_Click
    Dim V As View = Sender
    Dim T As String = V.Tag
    Log (T)
End Sub
 

emexes

Well-Known Member
Licensed User
It works better with sender than Me
You're 100% correct. I'm going to blame that mistake on multilanguage crosstalk :)

The sad thing is, I actually went into B4A to check that I was using the right word, checked "me" first and read "returns a reference to the current instance" which sounded like what I was looking for. I should have spent an extra five seconds checking "sender" too.
 

ALBRECHT

Active Member
Licensed User
no blame, you have resolved my pb, it's the main !
Thanks Emexes
 
Top