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.










share|improve this question
























  • 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










  • 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















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.










share|improve this question
























  • 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










  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 2:34









Jayson Minard

36k14104170




36k14104170










asked Nov 7 at 19:11









jonasstr

134




134












  • 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










  • 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










  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini