Monday, November 25, 2013

Disable checkbox if X are checked in android [android help]


Disable checkbox if X are checked in android



I have 15 CheckBox and I must to stuck user when he checks more than 5 CheckBox. Is it possible? I used the method OnCheckedChangeListener to know if an item was checked... but I don't know how to make a limit after 5 items selected. See what I've tried to do below:


I instante my integer:



int lengthBox = 15;
int lenghtMax = 5;


I select all my View in onCreate():



View[] tagsItem = new View[] {
findViewById(R.id.TagsCheckAA),
findViewById(R.id.TagsCheckBB),
findViewById(R.id.TagsCheckCC),
findViewById(R.id.TagsCheckDD),
findViewById(R.id.TagsCheckEE),
findViewById(R.id.TagsCheckFF),
findViewById(R.id.TagsCheckGG),
findViewById(R.id.TagsCheckHH),
findViewById(R.id.TagsCheckII),
findViewById(R.id.TagsCheckJJ),
findViewById(R.id.TagsCheckKK),
findViewById(R.id.TagsCheckLL),
findViewById(R.id.TagsCheckMM),
findViewById(R.id.TagsCheckNN),
findViewById(R.id.TagsCheckOO)
};


I create 15 CheckBox:



final CheckBox[] tagsCheck = new CheckBox[lengthBox];


Create the method (and it's here where I don't know what to do exactly):



OnCheckedChangeListener checker = new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if(tagsCheck[0].isChecked() || tagsCheck[1].isChecked() ||
tagsCheck[2].isChecked() || tagsCheck[3].isChecked() ||
tagsCheck[4].isChecked() || tagsCheck[5].isChecked() ||
tagsCheck[6].isChecked() || tagsCheck[7].isChecked() ||
tagsCheck[8].isChecked() || tagsCheck[9].isChecked() ||
tagsCheck[10].isChecked() || tagsCheck[11].isChecked() ||
tagsCheck[12].isChecked() || tagsCheck[13].isChecked() ||
tagsCheck[14].isChecked()) {
if(lenghtCount < 5){
// How can I get the String of the CheckBox
// which just checked?
String tags = (String) tagsCheck[].getText();
Toast.makeText(getApplicationContext(),
tags + " checked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),
"Limit reached!!!", Toast.LENGTH_SHORT).show();
}
}
}
};


After this, I set my View id to my CheckBox and call the method:



for(int i = 0; i < lengthBox; i++) {
tagsCheck[i] = (CheckBox) tagsItem[i];
tagsCheck[i].setOnCheckedChangeListener(checker);
}


Can someone point me in the right way, please?


Thanks, any help will be appreciate.



UPDATE:


I found an inelegant way with an if(tagsCheck[0].isChecked() || ...). But I have still a problem: how can I get the CheckBox which just checked? Cause of my if(), I don't know how can I do this.


Thanks!



Read more

stackoverflow.comm



No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...