Android Question Limited multiple checkbox selection

Hi everyone, this is my first post on the forum, I am still a newbie to b4x programming and I know my questions might be silly but I have been looking for the solution to my problem in the numerous b4a and b4x guides, searching the forum but I haven't quite found a proper answer to my question.
Here is what I'm trying to achieve : I have 27 items represented as checkboxes, all loaded in the Designer and I want the user to select only 7 of them. When he reaches this limit he cannot check further boxes, the user gets a toast message saying he can only select 7 checkboxes and he must first uncheck the previously selected boxes in order to select other boxes until reaching again the limit of 7 and so on.

So I proceeded like this : I changed the event name of all my 27 checkboxes into "chk" and created a sub called chk_CheckedChange. In this sub I created a "virtual" checkbox so every time a checkbox is checked, the number of selected checkboxes increases until reaching the limit of 7. Here is my code :

B4X:
Sub chk_CheckedChange(Checked As Boolean)
    Dim virtual_chk As CheckBox
    
    Log(nbr_chk_selected)
    virtual_chk = Sender
    
    If virtual_chk.Checked = True And nbr_chk_selected >= 7 Then
    virtual_chk.Checked = False

    ToastMessageShow("You cannot select more than 7 checkboxes.", False)
    Else
    End If     
        
        If virtual_chk.Checked Then
            nbr_chk_selected = nbr_chk_selected + 1
            Else
        End If
End Sub

And what it does is, I can check my checkboxes until I reach the limit of 7, then I receive the toast message, but I cannot check other checkboxes even after i uncheck the previous ones.
As I said, I've tried to find a solution on the forum without success, so I tried figuring it out myself but I cannot understand where the problem is. I think it's just a simple element I didn't add but I can't quite find what it is. Thank you in advance.
 

DonManfred

Expert
Licensed User
Longtime User
Put your checkboxes into an array after you load the layout containing all of them.
In chk_CheckedChange you need to iterate though the array and check the selected state of all Checkboxes.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private CheckBox1 As CheckBox
    Private CheckBox10 As CheckBox
    Private CheckBox2 As CheckBox
    Private CheckBox3 As CheckBox
    Private CheckBox4 As CheckBox
    Private CheckBox5 As CheckBox
    Private CheckBox6 As CheckBox
    Private CheckBox7 As CheckBox
    Private CheckBox8 As CheckBox
    Private CheckBox9 As CheckBox
    Private checkboxes As List
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")
    Sleep(250)
    checkboxes.Initialize
    checkboxes.Add(CheckBox1)
    checkboxes.Add(CheckBox2)
    checkboxes.Add(CheckBox3)
    checkboxes.Add(CheckBox4)
    checkboxes.Add(CheckBox5)
    checkboxes.Add(CheckBox6)
    checkboxes.Add(CheckBox7)
    checkboxes.Add(CheckBox8)
    checkboxes.Add(CheckBox9)
    checkboxes.Add(CheckBox10)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub chk_CheckedChange(Checked As Boolean)
    Dim selected As Int = 0
    Dim chk As CheckBox  = Sender

    For i= 0 To checkboxes.Size-1
        Dim check As CheckBox = checkboxes.Get(i)
        Log(check)
        Log(check.IsInitialized)
        If check.Checked Then
            selected = selected +1
        End If

      
    Next
    If selected > 7 Then
        chk.Checked = False
       slected = selected -1
    End If

  
    Log($"Selected: ${selected}"$)
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
This works:
When Checked = False you need to decrement nbr_chk_selected !
B4X:
Sub chk_CheckedChange(Checked As Boolean)
    If Checked = True
        If nbr_chk_selected < 7 Then
            nbr_chk_selected = nbr_chk_selected + 1
        Else
            Private chk As CheckBox
            chk = Sender
            chk.Checked = False
            ToastMessageShow("You cannot select more than 7 checkboxes.", False)
        End If     
    Else    
        nbr_chk_selected = nbr_chk_selected - 1
    End If     
End Sub
 
Upvote 0
Thank you all for the quick reply, @klaus I tried your method and it works indeed here is the code I used for those who might be interested :

B4X:
Sub chk_CheckedChange(Checked As Boolean)
    Log(nbr_chk_selected)
If Checked = True And nbr_chk_selected < 7 Then
    nbr_chk_selected = nbr_chk_selected + 1
Else
    Private chk As CheckBox
    chk = Sender
    chk.Checked = False
    ToastMessageShow("You cannot select more than 7 checkboxes.", False)
End If
If Checked = False Then
nbr_chk_selected = nbr_chk_selected - 1
End If
End Sub

Thanks again for fixing my problem, you saved me precious hours !
 
Upvote 0
Top