Android Question how to create command button inside JobDone function

Sofiya

Member
Licensed User
i want to create a some array of buttons during the job done function, from my knowledge i don't think its possible to create button outside of Activity_Create, is i have to call activity create function from job done, in that case how can i set the text for array of buttons (i mean can i pass parameter in activity create function), or else hope some other way we can create button array from job done, pls guide me, thanks in advance
 

LucaMs

Expert
Licensed User
Longtime User
from my knowledge i don't think its possible to create button outside of Activity_Create,
Why? You can create any view when and where you need.

At least you will have to declare an Array or List or Map in the Globals to "hold" the new views.

Create a Sub to add a new view, like:

B4X:
Sub AddNewButton(pnlParent As Panel, EventName As String, Left As Int, Top As Int, Width As Int, Height As Int, Text As String) As Button
    Dim btn As Button
    btn.Initialize(EventName)
    pnlParent.AddView(btn, Left, Top, Width, Height)
    btn.Text = Text
    Return btn
End Sub

lstButtons.Add(AddNewButton(...))

[pnlParent can be Activity]
 
Upvote 0

Claudio Oliveira

Active Member
Licensed User
Longtime User
Hi Sofiya,
You can create an array of Buttons, like this:
B4X:
Dim Buttons() as Button
Later on, you can access, initialize and customize each individual button through its index:
B4X:
Buttons(0).Initialize("Buttons")
Buttons(0).Tag = 0 'This is to identify whichbutton was clicked
Buttons(0).Color=...
Buttons(0).whatever...

Activity.AddView(Buttons(0), Left, Top, Width, Height)

And so on...

To catch click event, use Sender
B4X:
Sub Buttons_Click
    Dim b as Button = Sender

    Select b.tag 'Which button was clicked?
        Case 0
            Whatever code Button(0) should execute
        Case 1
            Whatever code Button(1) should execute
        ...
    End Select

End Sub

Try this. It should work...
 
Upvote 0

Claudio Oliveira

Active Member
Licensed User
Longtime User
You can also create a Click event for each individual Button:
B4X:
Button(0).Initialize("Btn0")
Button(1).Initialize("Btn1")
Button(2).Initialize("Btn2")
I prefer creating a single Click sub, and deal with the different buttons inside it.
 
Upvote 0

Sofiya

Member
Licensed User
At least you will have to declare an Array or List or Map in the Globals to "hold" the new views.

Create a Sub to add a new view, like:

B4X:
Sub AddNewButton(pnlParent As Panel, EventName As String, Left As Int, Top As Int, Width As Int, Height As Int, Text As String) As Button
    Dim btn As Button
    btn.Initialize(EventName)
    pnlParent.AddView(btn, Left, Top, Width, Height)
    btn.Text = Text
    Return btn
End Sub
lstButtons.Add(AddNewButton(...))

Sorry for late reply, i try this solution but nothing is created in my activity. It display blank layout.
 
Upvote 0

Sofiya

Member
Licensed User
At least you will have to declare an Array or List or Map in the Globals to "hold" the new views.

Create a Sub to add a new view, like:

B4X:
Sub AddNewButton(pnlParent As Panel, EventName As String, Left As Int, Top As Int, Width As Int, Height As Int, Text As String) As Button
    Dim btn As Button
    btn.Initialize(EventName)
    pnlParent.AddView(btn, Left, Top, Width, Height)
    btn.Text = Text
    Return btn
End Sub
lstButtons.Add(AddNewButton(...))

Now it's work fine. Thank u so much for your support.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi LucaMs, is there any option to set a name of an individual button.?
Use the "Tag" property to store the name/id of the button. Of course you'll have to loop through the views in order to find the button of interest. Best thing is use a prefix for all your button names e.g "btnButton1". This way when you loop through the views you can ignore any that don't begin with "btn" thus saving some time.

Hope this helps.

Thanks
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Forgot to quote LucaMs code change;

Why? You can create any view when and where you need.

At least you will have to declare an Array or List or Map in the Globals to "hold" the new views.

Create a Sub to add a new view, like:

B4X:
Sub AddNewButton(pnlParent As Panel, EventName As String, Left As Int, Top As Int, Width As Int, Height As Int, Text As String, ID as String) As Button
    Dim btn As Button
    btn.Initialize(EventName)
    pnlParent.AddView(btn, Left, Top, Width, Height)
    btn.Text = Text
    btn.Tag = ID '<<<<< add this line
    Return btn
End Sub

lstButtons.Add(AddNewButton(...))

[pnlParent can be Activity]
 
Upvote 0

Sofiya

Member
Licensed User
EventName As String

can u please tell me what event name should i use in the place of event name parameter, because i use the event name i.e. lstButtons_click but it's not work.

B4X:
Sub JobDone (job1 As HttpJob)
    Log("JobName = " & job1.JobName & ", Success = " & job1.Success)
    ProgressDialogHide
'    Job.GetRequest.Timeout = 60000
    If job1.Success Then
        Select job1.JobName
            Case "Outlet"
'                Log(job1.GetString)
                Dim parser As JSONParser
                Dim response As String = job1.GetString
                parser.Initialize(response)
                Dim rows As List
                rows = parser.NextArray
                For i = 0 To rows.Size - 1
                    Log("Rows #" & i)
                    Dim m As Map
                    m = rows.Get(i)
                    Log("posname=" & m.Get("posname"))
                    Log("poscode=" & m.Get("poscode"))
                    lstButtons.Initialize
                    lstButtons.Add(AddNewButton(Activity, "lstButtons_Click", 100dip, 40dip + 60dip * i, 120dip, 50dip, m.Get("posname")))
'                    StartActivity(Table)
                Next
        End Select
    End If
    job1.Release
End Sub

Sub lstButtons_Click
    Dim b As Button = Sender
    b.Text = ""
    StartActivity(Table)
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
can u please tell me what event name should i use in the place of event name parameter, because i use the event name i.e. lstButtons_click but it's not work.

lstButtons.Add(AddNewButton(Activity, "lstButtons_Click", 100dip, 40dip + 60dip * i, 120dip, 50dip, m.Get("posname")))
lstButtons.Add(AddNewButton(Activity, "PosButton", 100dip, 40dip + 60dip * i, 120dip, 50dip, m.Get("posname")))


Sub PosButton_Click
...
 
Upvote 0
Top