Maintaining the CheckBox Behaviour - android

I am having a select all checkbox button in my layout and a list view each with a check box in the right. I have added following code for the select all checkbox as :
selectall.setOnCheckChangedListener code :-
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(true);
}
Here I am maintaining each list view item
holder.mcbGoupMember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
commonFriends.get(position).setChecked(isChecked);
}
});
Now here I am stuck in a situation, I want that when I manually select all the checkbox items, then at that instant the select all button/checkbox should automatically be checked on and vice versa.
How can I achieve this??

Refer this sample program for listview checkbox scroll issue

You could just keep a simple count of the number of items you have checked at a time. If you check an item increase it by one, if you uncheck an item decrease it by one. After each increase/decrease check if this checked items number is equal to number of items on your list and check the select all if it is. Just make sure that your selectall.setOnCheckChangedListener sets the list checkbox values to whatever value it receives in isChecked. For example:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(isChecked);
}
}

Related

checkbox in custom ListView and selectAll button bug

I am facing a problem now that I really don't know what is going on.
First let me explain what I want to do.
I am having a custom ListView that show my user detail and a checkbox beside it. At first, if I checked all the checkbox, the selectAll checkbox will not check on by itself.
Therefore, I have implemented the codes below. It works perfectly at first, but in the end I found out that if I have too much item that require me to scroll down, the system like detected that I have uncheck the checkbox and help me to deduct my "count".
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView == selectAll){
selectAll.setChecked(isChecked);
for(int i = 0; i < theList.size(); i++){
theList.get(i).setSelected(isChecked);
theList.get(i).setCheckAll(isChecked);
}
notifyDataSetChanged();
}else{
int position2 = (Integer) buttonView.getTag();
if(isChecked){
theList.get(position2).setSelected(true);
System.out.println("count here positions:"+ position2);
count++;
System.out.println("count here:"+ count);
if(count == theList.size())
{
selectAll.setChecked(true);
}
//String this_is_the_comment = theList.get(position2).getApprComments();
//System.out.println(this_is_the_comment);
}else{
theList.get(position2).setSelected(false);
count--; //when i scroll down the if condition will come to here
System.out.println("count here: -- "+count);
if (selectAll.isChecked()) {
selectAll.setChecked(false);
for (int i = 0; i < theList.size(); i++) {
theList.get(i).setSelected(true);
theList.get(position2).setSelected(false);
}
}
}
}
Please kindly help me with this bug. I am lost and stuck for 3 days.
in onCheckedChanged method at last you need to check if all the items are checked if yes then perform selectAll.setChecked(true);
add this at last in your method
boolean allChecked=true;
for(int i = 0; i < theList.size(); i++){
if(theList.get(i).isSelected){
checker=false;
}
}
if(allChecked){
selectAll.setChecked(true);
}

How can I get the selected value of the checked checkboxes written programmatically in android?

I have a CheckBox written programmatically in android, it is programmatically written because the value of the CheckBox will be based on online content, so basically, the program has no idea on how many CheckBox will going to print. I use for loops to print checkboxes, and it was successful, but among those checkboxes, I want to get the value of the CheckBox that will going to be selected.
How can I do that?
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
CheckBox cb = new CheckBox(survey);
cb.setText(ansar[z]);
choiceslayouts.addView(cb);
}
You need to create a boolean array for saving the state of all checkbox
Then add setOnCheckedChangeListener for each CheckBox
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
boolean[] array = new boolean[ansar.length];
for (int z = 0; z<ansar.length; z++){
...
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
array[z] = isCheck;
}
});
}
OR
You can set the tag for each CheckBox to check the state of CheckBox
1.Implement onClickListener for your Activity
public YourActivity extends Activity implements View.OnClickListener
2.Then set the tag for each CheckBox
for (int z = 0; z<ansar.length; z++){
...
cb.setTag("tag"+z);
cb.setOnClickListener(this);
}
3.Finnaly, implement onClick
public void onClick(View v) {
switch (v.getTag()) {
case "tag0":
if(((CheckBox) v).isChecked()) {
// your checkbox 0 is checked
}else{
// your checkbox 0 is not checked
}
break;
case "tag1":
// do the same
break;
...
}
}
Hope this help
I think you should use a ListView or a RecyclerView with custom layout and custom adapter, in order to optimize performance. Anyway it should be possible to achieve what you're look for without a major change to your code.
Your problem is that in the for loop you recreate a new checkbox with the exact same properties as the previous one, so even if you add it to your parent view, you are basically adding the same checkbox over and over again, and you can't listen to the event in the "single" checkboxes.
You should try something like this:
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
final boolean[] cbValue = new boolean[ansar.length];
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
cb[z] = new CheckBox(survey);
cb[z].setText(ansar[z]);
cb[z].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cbValue[z] = !cbValue[z];
}
});
choiceslayouts.addView(cb[z]);
}
cbValue[] should hold the values of your checkboxes.
This is without error-checking, but it should give you the idea.

How to check if more than 5 checkbox is checked in android?

As the title says, I want to check if more than 5 check box is checked in android. What I want to do is to check 5 checkbox and if I enter more or less than 5 checkbox it will show a alert . How will I do this ? I beleive that the pros here can help a noob like me. A example will be helpful for me to learn.
for(int i = 0; i< checkbox.length;i++){
checkbox[i] = (CheckBox)findViewById(ids[i]);
checkbox[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) { buttonView.setTextColor(Color.BLACK);
buttonClicked.add(buttonView.getText().toString());
}
if (!isChecked) {buttonView.setTextColor(Color.GREEN);
buttonClicked.remove(buttonView.getText().toString());
}
}
});
Set a counter that increases by 1 each time a checkbox is checked, and decreases by 1 each time a checkbox is unchecked. If the counter is > 5 then there are more than 5 checkboxes checked and you can display your alert.

How to handle check and uncheck dynamically created checkbox in android

i have created check boxes using loop and i want to validate it. Like i just want to check only 3 from the check boxes , when i press on the 4th one it should show an alert and uncheck it.
And i am able to get the alert when i press the 4the one but it is not unchecking.
anybody faced such issue and how did you solve it ?
int i;
for (i = 0; i < 20; i++) {
CheckBox ch = new CheckBox(this);
ch.setTag(Integer.valueOf(i));
ch.setText("CheckBox " + i);
ch.setChecked(false);
ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
numChecked++;
} else {
numChecked--;
}
if (numChecked == 4) {
buttonView.setChecked(false);
numChecked--;
// fourth one selected, show your dialog
}
}
});
}
You will also need a global variable call numChecked:
int numChecked = 0;
You will also need to add a .addView(ch) in the loop's end to add the CheckBoxes to your layout.

Make a Layout inactive in android

I have a RelativeLayout and in this RelativeLayout there are 4 Buttons. Outside this RelativeLayout there is CheckBox.The whole View is in a RelativeLayout.
I want to make the 4 Buttons to inactive (which are present in the RelativeLayout) by selecting on CheckBox and I want to make all the buttons active when I again select the CheckBox. so what to do ?
I have also tried relativeLayout.setClickable(false); but it is not working.
final int[] BUTTON_IDS = { R.id.button1, R.id.button2, };
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
for (int btnId = 0; btnId < BUTTON_IDS.length; btnId++) {
Button btn = (Button) findViewById(btnId);
btn.setEnabled(isChecked);
}
}
});
UTDATE
Try this:
<yourRelativeLayout>.setEnabled(false);
or you can change state of all your buttons to disabled.

Categories

Resources