OnclickListener in checkbox (Android Studio)… here is a solution to the problem.
OnclickListener in checkbox (Android Studio)
Here is part of my code, which includes CheckBox(ChckBoxNo):
final CheckBox ChckBoxNo = (CheckBox)promptsView.findViewById(R.id.ChkBoxNo);
ChckBoxNo.setChecked(true);
ChckBoxNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ChckBoxNo.isChecked()) {
ChckBoxNo.setChecked(false);
}
else if (! ChckBoxNo.isChecked())
{
ChckBoxNo.setChecked(true);
}
}
});
At first I set true for the isChecked() method on my checkbox, then I implemented onclicklistener on my checkbox.
When I run the application, the checkbox is checked as I defined the eralier, but when I click on the checkbox, it is not checked and then immediately checked again (I don’t hit the checkbox again!)
What should I do to fix this, what’s wrong with my code?
Thanks!
Solution
Try this :
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setChecked(checkPasswordExist());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox.isChecked()) {
your code to checked checkbox
}
} else {
your code to no checked checkbox
}
}
});