iOS Question How can I create a dynamic checkbox? Do I use the segment control

Brian Robinson

Active Member
Licensed User
Longtime User
iOS has no checkbox, so A SegmentedControl would probably suit what you want to do.

Just need to create a List of items to display and set it using

B4X:
    Dim sc as SegmentedControl
    sc.Initialize("sc")
    Dim items() As String = Array As String("A","B","C")
    sc.SetItems(items)
 
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
Here is the final code
B4X:
sub Process_Globals
   Private listSegmentControl As List
end sub

Sub CreateSegmentControl

                Dim sc As SegmentedControl
                Dim oSCItems() As String=Array As String("Yes","No")
                sc.Initialize("seq"&iSegmentControlCounter)
                sc.Tag=sTestId
                sc.SetItems(oSCItems)
                If sTestIdValue="True" Then
                    sc.SelectedIndex=0
                Else
                    sc.SelectedIndex=1
                End If

ScrollViewInput.Panel.AddView(sc,200dip,iRowOffset,50%x,40dip)
                iRowOffset=iRowOffset+80
                listSegmentControl.Add(sc)

end sub

sub cmdSave_Click

Dim oSegmentControl As SegmentedControl
For i=0 To listSegmentControl.Size-1
   oSegmentControl=listSegmentControl.Get(i)
  sTestId=oSegmentControl.Tag
  If oSegmentControl.SelectedIndex=0 Then
    sTestValue="Y"
  Else
    sTestValue="N"
  End If
Next
end sub
 
Last edited:
Upvote 0

Brian Robinson

Active Member
Licensed User
Longtime User
I think for this instance of simply Yes/No a Switch view is better suited:
1. There are only two answers
2. The answers are Yes/No

If the two outcomes were Water/Milk then it makes sense to use a Segmented Control because you need the control to represent those two outcomes in their text.

Switch would be used for Yes/No, True/False, On/Off. The do look nicer on screen in those situations too.

Just my opinion.

Cheers
Brian
 
Last edited:
Upvote 0

Brian Robinson

Active Member
Licensed User
Longtime User
Hey Dave,

If you do decide to go with the switches, there are hardly any code changes involved as what you have already done is what is needed. Just need to swap out the controls.

* Note I don't have access to my compiler to see if this code actually runs, but will fix later when I do.

Similar to this:
B4X:
Sub Process_Globals
    ' .....
    Private listSwitchControl As List
End Sub

Private Sub CreateSwitchControl
    Dim sw As Switch
    sw.Initialize("sw"&iSwitchControlCounter)
    sw.Tag = sTestId ' I assume sTestId is defined elsewhere
    If sTestIdValue = "True" Then ' I assume sTestIdValue is defined elsewhere
        sw.Value = True
    Else
        sw.Value = False
    End If
   
    ScrollViewInput.Panel.AddView(sw,200dip,iRowOffset,50%x,40dip) ' I assume iRowOffset is defined elsewhere
    iRowOffset=iRowOffset+80
    listSwitchControl.Add(sw)

End Sub

Private Sub cmdSave_Click
    For Each sw As Switch In listSwitchControl
        If sw.Value = True Then
            sTestValue="Y" ' I assume this is a global somewhere
        Else
            sTestValue="N"
        End If
    Next
End Sub
 
Upvote 0
Top