I have in my application a number of checkboxes. The checkbox state can be changed either by clicking on tje checkbox or changing its state by code (checkbox.Checked = True/False).
The problem I have, is that both, a click on a checkbox and changing state by code trigger Sub cb1_CheckedChange(Checked As Boolean).
What I expected to see (and what I need) is that Sub cb1_CheckedChange(Checked As Boolean) is only triggered when I click on the checkbox.
When I change the state of the checkbox by code, I only want to change the state of the checkbox, but not trigger the event - because I didn't click on the checkbox.
To demonstrate this, I created a project with the following code:
Here is the log of the events when the Switch is clicked to simulate change the state of the checkbox by code:
I don't want to see an event being triggered "Checkbox Checked"/"Checkbox not Checked" in this situation.
I thought that maybe it is a normal Android behaviour, so I created a simple project in Android Studio with the same type of controls.
Relevant part of the java code is here:
And here is the log of the events when the switch is clicked:
The checkbox is activated, but no click event is registered when the state is changed by code (by clicking on a switch)
So, Android behaviour is what I have expected to be, and what I need.
Why is the B4A different?
Can I do it in B4A?
The problem I have, is that both, a click on a checkbox and changing state by code trigger Sub cb1_CheckedChange(Checked As Boolean).
What I expected to see (and what I need) is that Sub cb1_CheckedChange(Checked As Boolean) is only triggered when I click on the checkbox.
When I change the state of the checkbox by code, I only want to change the state of the checkbox, but not trigger the event - because I didn't click on the checkbox.
To demonstrate this, I created a project with the following code:
B4X:
Private cb1 As CheckBox
Private sw1 As ACSwitch
Private textView As Label
Sub cb1_CheckedChange(Checked As Boolean)
If Checked Then
ToastMessageShow("Checkbox checked", True)
textView.Text = "Checkbox Checked"
Log("Checkbox Checked")
Else
ToastMessageShow("Checkbox not checked", True)
textView.Text = "Checkbox not Checked"
Log("Checkbox Not Checked")
End If
End Sub
Sub sw1_CheckedChange(Checked As Boolean)
If Checked Then
ToastMessageShow("Swich On", True)
Log("Switch On, Checkbox set to Checked")
Log("-----------------------------")
cb1.Checked = True
Else
ToastMessageShow("Switch Off", True)
Log("Switch Off, Checkbox set to not Checked")
Log("-----------------------------")
cb1.Checked = False
End If
End Sub
Here is the log of the events when the Switch is clicked to simulate change the state of the checkbox by code:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Switch On, Checkbox set to Checked
-----------------------------
Checkbox Checked
Switch Off, Checkbox set to not Checked
-----------------------------
Checkbox Not Checked
I don't want to see an event being triggered "Checkbox Checked"/"Checkbox not Checked" in this situation.
I thought that maybe it is a normal Android behaviour, so I created a simple project in Android Studio with the same type of controls.
Relevant part of the java code is here:
B4X:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.cb1:
if(checkBox.isChecked()){
Toast.makeText(getApplicationContext(), "checkbox checked", Toast.LENGTH_LONG).show();
textView.setText("Checkbox Checked");
Log.d("B4A", "CheckBox Checked");
}
else{
Toast.makeText(getApplicationContext(), "checkbox not checked", Toast.LENGTH_LONG).show();
textView.setText("Checkbox not Checked");
Log.d("B4A", "CheckBox not Checked");
}
break;
case R.id.sw1:
if(aSwitch.isChecked()){
Toast.makeText(getApplicationContext(), "switch On", Toast.LENGTH_LONG).show();
checkBox.setChecked(true);
Log.d("B4A","Switch On, Checkbox set to Checked");
}
else{
Toast.makeText(getApplicationContext(), "switch Off", Toast.LENGTH_LONG).show();
checkBox.setChecked(false);
Log.d("B4A","Switch Off, Checkbox set to not Checked");
}
}
}
And here is the log of the events when the switch is clicked:
B4X:
Switch On, Checkbox set to Checked
Switch Off, Checkbox set to not Checked
So, Android behaviour is what I have expected to be, and what I need.
Why is the B4A different?
Can I do it in B4A?