I have an activity, which contains 4 checkboxes. So my question is - when all these checkboxes are checked, I want a message to pop-up that should say something like "Good job!", so my question is how do I do it ?
Asked
Active
Viewed 3,851 times
-3
-
its very simple, if all checkbox are checked then show message, to check check box is check or not use checkbox1.ischecked() == true. – Dhaval Parmar May 26 '15 at 20:21
-
**Code of the activity** I see nothing there **:)** – Bharatesh May 25 '16 at 05:26
-
Please add the code into your question as text instead of links to the images – fdermishin Dec 27 '20 at 17:52
2 Answers
0
Add this code in your onCreate method.
NOTE: You can use a checkbox array to short the code.
But this will help you to understand what to do.
First, you have to create checkbox variables in java file. Then set onCheckChanged listener to each checkbox.
CheckBox cb1=(CheckBox)findViewById(R.id.checkBox);
CheckBox cb2=(CheckBox)findViewById(R.id.checkBox2);
CheckBox cb3=(CheckBox)findViewById(R.id.checkBox3);
CheckBox cb4=(CheckBox)findViewById(R.id.checkBox4);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
Insert this import statements before the class begin (public class flygos2..) . You can see there are other import statements are there also.
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

fdermishin
- 3,519
- 3
- 24
- 45

Kasun Dissanayake
- 506
- 3
- 19
-
When im adding this code and changed to the right name on checkbox i get massive error on everything why ? – Kvenna May 16 '15 at 16:25
-
@Kvenna did you imported necessary libraries? What is the very first error you are getting. – Kasun Dissanayake May 16 '15 at 16:40
-
@Kvenna right side name checkbox should be checkBox. It should be capital B letter in checkbox because that is the ID of your first checkbox in the xml file. I'll edit the answer – Kasun Dissanayake May 16 '15 at 16:49
-
Im sorry im very new to android studio i only have experiance with html & css you see. How do i import to the librarie ? this error is showing up: http://gyazo.com/0e57091ddc4c006c4c037ffbf36d3606 – Kvenna May 16 '15 at 17:29
-
@Kvenna It's OK everyone has to start from the beginning :) I updated the answer. insert that statements before the class begins.. – Kasun Dissanayake May 16 '15 at 17:41
-
I also had to write "final" infront of the first declarance of the checkbox to make it work thanks mate! – Kvenna May 16 '15 at 17:56
-
Ah I forgot to add final keyword. It should be there because those variables are being used inside of those checkChanged listeners. Glad it worked for you :) – Kasun Dissanayake May 16 '15 at 18:00
0
- Add the following
updateMessage
in your java file. - Add
android:onClick="updateMessage"
to every checkbox in your XML file. By this, theupdateMessage
method will be called when we checked/un-checked the check box.
public void updateMessage(View view) {
String message = "";
CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox);
CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
if (cb1.isChecked() && cb2.isChecked() && cb3.isChecked() && cb4.isChecked()) {
message = "Good Job";
} else if (cb1.isChecked() && cb2.isChecked() && cb3.isChecked()) {
message = "One Check box is missed";
} else {
message = "try again";
}
}

DuDa
- 3,718
- 4
- 16
- 36

Rakesh Choudhury
- 1
- 1