Determine whether checkbox was clicked by the user or manually from code
up vote
0
down vote
favorite
I have multiple Check Boxes in an Android activity and I want to implement a tree-like functionality with these Check Boxes.
For example when the user clicks on a checkBox, all sub checkBoxes should also be toggled. Similarly, when all sub checkBoxes are in the same state, the parent checkBox should be notified about this and be toggled accordingly.
In this second case, the parent checkBox will be toggled but should not toggle all the child checkBoxes (because it was not actively clicked by the user but rather toggled as a consequence of all children being toggled)
I toggle the checkBoxes using checkBox.setChecked(isChecked)
.
This leads to the checkBox's OnCheckedChangeListener
being called which in turn calls a method responsible for updating all the children.
So I need to find a way to differentiate between user interaction and setting the checkBox state from code.
I tried using checkBox.setOnClickListener()
, however the onClickListener is always notified after the checkBox' state has already changed.
android user-interface checkbox kotlin
|
show 1 more comment
up vote
0
down vote
favorite
I have multiple Check Boxes in an Android activity and I want to implement a tree-like functionality with these Check Boxes.
For example when the user clicks on a checkBox, all sub checkBoxes should also be toggled. Similarly, when all sub checkBoxes are in the same state, the parent checkBox should be notified about this and be toggled accordingly.
In this second case, the parent checkBox will be toggled but should not toggle all the child checkBoxes (because it was not actively clicked by the user but rather toggled as a consequence of all children being toggled)
I toggle the checkBoxes using checkBox.setChecked(isChecked)
.
This leads to the checkBox's OnCheckedChangeListener
being called which in turn calls a method responsible for updating all the children.
So I need to find a way to differentiate between user interaction and setting the checkBox state from code.
I tried using checkBox.setOnClickListener()
, however the onClickListener is always notified after the checkBox' state has already changed.
android user-interface checkbox kotlin
One can (temporarily) remove theOnCheckedChangeListener
by callingcheckBox.setOnCheckedChangeListener(null);
Later on you just set the original Listener (which for this purpose should be a field of your Activity or Fragment) once more.
– 0X0nosugar
Nov 7 at 19:53
I don't understand what's the problem with onclick being called after checkbox state changes
– Pawel
Nov 7 at 23:53
Maybe it would cause an endless loop @Pawel
– Jayson Minard
Nov 8 at 2:34
@Pawel If it's called afterwards the children are already notified to update which I don't want in this case
– jonasstr
Nov 8 at 8:16
@0X0nosugar Thank you. I thought about doing this and will try it out.
– jonasstr
Nov 8 at 8:16
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have multiple Check Boxes in an Android activity and I want to implement a tree-like functionality with these Check Boxes.
For example when the user clicks on a checkBox, all sub checkBoxes should also be toggled. Similarly, when all sub checkBoxes are in the same state, the parent checkBox should be notified about this and be toggled accordingly.
In this second case, the parent checkBox will be toggled but should not toggle all the child checkBoxes (because it was not actively clicked by the user but rather toggled as a consequence of all children being toggled)
I toggle the checkBoxes using checkBox.setChecked(isChecked)
.
This leads to the checkBox's OnCheckedChangeListener
being called which in turn calls a method responsible for updating all the children.
So I need to find a way to differentiate between user interaction and setting the checkBox state from code.
I tried using checkBox.setOnClickListener()
, however the onClickListener is always notified after the checkBox' state has already changed.
android user-interface checkbox kotlin
I have multiple Check Boxes in an Android activity and I want to implement a tree-like functionality with these Check Boxes.
For example when the user clicks on a checkBox, all sub checkBoxes should also be toggled. Similarly, when all sub checkBoxes are in the same state, the parent checkBox should be notified about this and be toggled accordingly.
In this second case, the parent checkBox will be toggled but should not toggle all the child checkBoxes (because it was not actively clicked by the user but rather toggled as a consequence of all children being toggled)
I toggle the checkBoxes using checkBox.setChecked(isChecked)
.
This leads to the checkBox's OnCheckedChangeListener
being called which in turn calls a method responsible for updating all the children.
So I need to find a way to differentiate between user interaction and setting the checkBox state from code.
I tried using checkBox.setOnClickListener()
, however the onClickListener is always notified after the checkBox' state has already changed.
android user-interface checkbox kotlin
android user-interface checkbox kotlin
edited Nov 8 at 2:34
Jayson Minard
36k14104170
36k14104170
asked Nov 7 at 19:11
jonasstr
134
134
One can (temporarily) remove theOnCheckedChangeListener
by callingcheckBox.setOnCheckedChangeListener(null);
Later on you just set the original Listener (which for this purpose should be a field of your Activity or Fragment) once more.
– 0X0nosugar
Nov 7 at 19:53
I don't understand what's the problem with onclick being called after checkbox state changes
– Pawel
Nov 7 at 23:53
Maybe it would cause an endless loop @Pawel
– Jayson Minard
Nov 8 at 2:34
@Pawel If it's called afterwards the children are already notified to update which I don't want in this case
– jonasstr
Nov 8 at 8:16
@0X0nosugar Thank you. I thought about doing this and will try it out.
– jonasstr
Nov 8 at 8:16
|
show 1 more comment
One can (temporarily) remove theOnCheckedChangeListener
by callingcheckBox.setOnCheckedChangeListener(null);
Later on you just set the original Listener (which for this purpose should be a field of your Activity or Fragment) once more.
– 0X0nosugar
Nov 7 at 19:53
I don't understand what's the problem with onclick being called after checkbox state changes
– Pawel
Nov 7 at 23:53
Maybe it would cause an endless loop @Pawel
– Jayson Minard
Nov 8 at 2:34
@Pawel If it's called afterwards the children are already notified to update which I don't want in this case
– jonasstr
Nov 8 at 8:16
@0X0nosugar Thank you. I thought about doing this and will try it out.
– jonasstr
Nov 8 at 8:16
One can (temporarily) remove the
OnCheckedChangeListener
by calling checkBox.setOnCheckedChangeListener(null);
Later on you just set the original Listener (which for this purpose should be a field of your Activity or Fragment) once more.– 0X0nosugar
Nov 7 at 19:53
One can (temporarily) remove the
OnCheckedChangeListener
by calling checkBox.setOnCheckedChangeListener(null);
Later on you just set the original Listener (which for this purpose should be a field of your Activity or Fragment) once more.– 0X0nosugar
Nov 7 at 19:53
I don't understand what's the problem with onclick being called after checkbox state changes
– Pawel
Nov 7 at 23:53
I don't understand what's the problem with onclick being called after checkbox state changes
– Pawel
Nov 7 at 23:53
Maybe it would cause an endless loop @Pawel
– Jayson Minard
Nov 8 at 2:34
Maybe it would cause an endless loop @Pawel
– Jayson Minard
Nov 8 at 2:34
@Pawel If it's called afterwards the children are already notified to update which I don't want in this case
– jonasstr
Nov 8 at 8:16
@Pawel If it's called afterwards the children are already notified to update which I don't want in this case
– jonasstr
Nov 8 at 8:16
@0X0nosugar Thank you. I thought about doing this and will try it out.
– jonasstr
Nov 8 at 8:16
@0X0nosugar Thank you. I thought about doing this and will try it out.
– jonasstr
Nov 8 at 8:16
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196223%2fdetermine-whether-checkbox-was-clicked-by-the-user-or-manually-from-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
One can (temporarily) remove the
OnCheckedChangeListener
by callingcheckBox.setOnCheckedChangeListener(null);
Later on you just set the original Listener (which for this purpose should be a field of your Activity or Fragment) once more.– 0X0nosugar
Nov 7 at 19:53
I don't understand what's the problem with onclick being called after checkbox state changes
– Pawel
Nov 7 at 23:53
Maybe it would cause an endless loop @Pawel
– Jayson Minard
Nov 8 at 2:34
@Pawel If it's called afterwards the children are already notified to update which I don't want in this case
– jonasstr
Nov 8 at 8:16
@0X0nosugar Thank you. I thought about doing this and will try it out.
– jonasstr
Nov 8 at 8:16