I have two check boxes in a alert dialog box. What I want is that if one of the checkbox is clicked then other checkbox gets unchecked automatically. Only one checkbox can be clicked at a time. I've searched the internet and tried multiple solutions but no solution is working.
find ids of your check boxs...
checkBoxOne= (CheckBox)findViewById(R.id.checkBox1);
checkBoxTwo= (CheckBox)findViewById(R.id.checkBox2);
checkBoxOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Here you unchecked the other check boxes
checkBoxTwo.setChecked(false);
}
}
});
checkBoxTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Here you unchecked the other check boxes
checkBoxOne.setChecked(false);
}
}
});
If you really want to use a CheckBox you have to programmatically uncheck the other button:
private void createCustomDialogCheckBox() {
LinearLayout dialogLayout = new LinearLayout(this);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
final CheckBox checkBoxOption1 = new CheckBox(this);
final CheckBox checkBoxOption2 = new CheckBox(this);
checkBoxOption1.setText("Option 1");
checkBoxOption2.setText("Option 2");
checkBoxOption1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox otherCheckBox = checkBoxOption2;
if(isChecked && otherCheckBox.isChecked()){
otherCheckBox.setChecked(!otherCheckBox.isChecked());
}
}
});
checkBoxOption2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox otherCheckBox = checkBoxOption1;
if(isChecked && otherCheckBox.isChecked()){
otherCheckBox.setChecked(!otherCheckBox.isChecked());
}
}
});
dialogLayout.addView(checkBoxOption1);
dialogLayout.addView(checkBoxOption2);
dialogBuilder.setView(dialogLayout);
dialogBuilder.show();
}
A better solution is to use RadioButton with does all this automatically:
private void createCustomDialogRadioButton() {
LinearLayout dialogLayout = new LinearLayout(this);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
RadioButton radioButtonOption1 = new RadioButton(this);
RadioButton radioButtonOption2 = new RadioButton(this);
radioButtonOption1.setText("Option 1");
radioButtonOption2.setText("Option 2");
RadioGroup radioGroup = new RadioGroup(this);
// radio buttons must be on the same radio group
// so that other buttons will automatically be unchecked / unselected
radioGroup.addView(radioButtonOption1);
radioGroup.addView(radioButtonOption2);
dialogLayout.addView(radioGroup);
dialogBuilder.setView(dialogLayout);
dialogBuilder.show();
}
Related
I have 3 checkbox in my Layout:
(case #1) mCheckBoxAll: when is checked or unchecked, all other boxes are set in the same state.
(case #2) mCheckBoxChoice1 and mCheckBoxChoice2: if one of these is unchecked by the user then the mCheckBoxAll must be unchecked.
mCheckBoxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxChoice1.setChecked(isChecked);
mCheckBoxChoice2.setChecked(isChecked);
}
});
mCheckBoxChoice1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked)
mCheckBoxAll.setChecked(!isChecked);
}
});
mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked)
mCheckBoxAll.setChecked(!isChecked);
}
});
But this code doesn't work in case #2. All cases are unchecked in the same time...
I would put all your checkboxes that should change at once in a RadioGroup, and when mCheckBoxAll is checked iterate over RadioGroup and select / deselect them all
case 1
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
for (int i = 0; i < radioGroup.getChildCount(); i++){
((RadioButton) radioGroup.getChildAt(i)).setChecked(isChecked);
}
case 2 - you could call this method, to determine if one of the buttons is unchecked (call inside your 2 buttons onCheckedChanged). If it returns false, deselecte the main RadioButton.
private boolean isButtonUnchecked() {
for (int i = 0; i < radioGroup.getChildCount(); i++){
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if (!radioButton.isChecked()) {
return false;
}
}
return true;
}
When you checkbox2 is unchecked then you set the state of checkboxall to be unchecked, and there you call checkbox1 to be uncheck as well, here is your problem.
So if checkbox 1 and 2 are checked and then you unchecked checkbox2 it will affect checkbox1.
Edit:
You should identify the source of the unchecked in checkboxall, if it comes from one of the two checkbox or if it comes from the uncheck all.
The easiest solution is to declare a global variable in your activity/fragment which tells you if you comes from checkboxes 1 and 2.
And if your checkboxall listener uncheck 1 and 2 only if the value is true.
Don't forget to change its value when you are done to false.
Edited code:
modify your code to
mListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxChoice1.setChecked(isChecked);
}
};
mCheckBoxAll.setOnCheckedChangeListener(mListener);
mCheckBoxChoice1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
mCheckBoxAll.setOnCheckedChangeListener(null);
mCheckBoxAll.setChecked(!isChecked);
mCheckBoxAll.setOnCheckedChangeListener(mListener)
}
}
});
mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
mCheckBoxAll.setOnCheckedChangeListener(null);
mCheckBoxAll.setChecked(!isChecked);
mCheckBoxAll.setOnCheckedChangeListener(mListener)
}
}
});
use below code and it will work your scenario
int mCheckBoxAll_clicktimes=0;
int mCheckBoxChoice2_clicktimes=0;
int mCheckBoxChoice1_clicktimes=0;
mCheckBoxAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes++;
mCheckBoxChoice2_clicktimes=0;
mCheckBoxChoice1_clicktimes=0;
if(mCheckBoxAll_clicktimes%2 !=0 || mCheckBoxAll_clicktimes==0){
//-----when mcheckbox all checked
mCheckBoxAll.setChecked(true);
mCheckBoxChoice1.setChecked(true);
mCheckBoxChoice2.setChecked(true);
}else{
//===when mcheckboxall unchecked
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
}
}
});
mCheckBoxChoice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes=0;
mCheckBoxChoice2_clicktimes=0;
mCheckBoxChoice1_clicktimes++;
if(mCheckBoxChoice1_clicktimes%2 !=0 || mCheckBoxChoice1_clicktimes==0){
//when mCheckBoxChoice1 checked
mCheckBoxChoice1.setChecked(true);
}else{
//====when mCheckBoxChoice1 unchecked
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
}
}
});
mCheckBoxChoice2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes=0;
mCheckBoxChoice2_clicktimes++;
mCheckBoxChoice1_clicktimes=0;
if(mCheckBoxChoice2_clicktimes%2 !=0 || mCheckBoxChoice2_clicktimes==0){
//when mCheckBoxChoice2 checked
mCheckBoxChoice2.setChecked(true);
}else{
//when mCheckBoxChoice2 unchecked
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll.setChecked(false);
}
}
});
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 ?
Code of the activity
Code of the checkboxes
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;
Add the following updateMessage in your java file.
Add android:onClick="updateMessage" to every checkbox in your XML file. By this, the updateMessage 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";
}
}
I have an activity that extends PreferenceActivity.
I have in settings.xml 3 checkboxpreferences. If I uncheck the first checkbox, the other 2 to uncheck.
What is the code for that?
I think you want to use checkbox.toggle()
First make a onClickListener:
Checkbox chk = (CheckBox)findViewById(R.id.checkBox1);
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
);
Then change the other checkboxes:
CheckBox chk1, chk2;
chk1 = (CheckBox)findViewById(R.id.checkBox2);
chk2 = (CheckBox)findViewById(R.id.checkBox3);
and:
if(chk1.isChecked()){
chk1.toggle();
}
if(chk2.isChecked()){
chk2.toggle();
}
I have a number of check boxes creating dynamically.
Now i just want to select any of them not more than one. When i select the second one the first one should unselect.
I can't use radio group. Any help
Here is my code .. i am creating check boxes dynamically
private void createRadioButton() {
cb = new CheckBox(fContext);
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
Log.i("comVisa","getPos =="+getPosition);
}
});
}
You can save last check box checked and unchecked it when checked other one like this
In public area define this
HashMap<String, CompoundButton> hash = new HashMap<String, CompoundButton>();
then edit your code :
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//int getPosition = (Integer) buttonView.getTag();
if(isChecked){
if(hash.size()>0){
hash.get("1").setChecked(false);
}
hash.put("1", buttonView);
}else{
hash.clear();
}
Log.i("comVisa","getPos =="+getPosition);
}
});
A really simple example of using single selection checkbox. try this link
CheckBox myCheckBox[] = new CheckBox[noofCheckBox];
for (int i=0; i<noofCheckBox; i++) {
myCheckBox[i] = new myCheckBox(this);
myCheckBox[i].setId(i+1);
myCheckBox[i].setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if(myCheckBox[i].getId() == getPosition){
myCheckBox[i].setChecked(true);
}else{
myCheckBox[i].setChecked(false)
}
}
});
}
How can I display a pop-up immediately a checkbox is checked in Android?
CheckBox yourCheckBox = (CheckBox) findViewById( R.id.yourCheckBox );
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
//call popup
}
}
});
You just need to declare a call back for the CheckedChanged Event on the text box and then fire your method that displays the popup