Dynamically setting CheckBox and get if all checkbox is checked - android

ArrayList<String> toDoArray = XYZ.getChecklistItems(reasonToCal);
for (int i = 0; i < toDoArray.size(); i++) {
final CheckBox checkbox = new CheckBox(this);
checkbox.setText(toDoArray.get(i));
checkbox.setTextColor(Color.parseColor("#FFFFFF"));
checkLinearLayout.addView(checkbox);
}
I am dynamically setting checkbox in my layout.
The problem I am facing is i cannot get if all my checkbox is checked so that i can enable the button if all checkboxes are checked

I guess you can implement common listener for all check-boxes which will increase variable of checked check-boxes and also decrease is user will uncheck any you should also keep somewhere total numbers of check-boxes and compare those two variables - if equals set enabled on button.
That can be one approach.

Related

Set Id to dynamically created edittext

I'm dynamically creating Edittext on a button click and saving this edittext to an ArrayList of type Edittext and then getting values of all edittext in an ArrayList of type String. While creating new Edittext Iam also creating a BUTTON with cross image which delete that edittext when user click on it. and then Iam passing these edittexts to Options of CheckBox question which is also Iam creating dynamically.
Issue is when I create multiple edittext and randomly delete someone and click ok arraylist remove the last index of it .Iam also setting ids to Edittext but couldnt able to understand how to get the Id of the same Edittext whose value I want to delete and then remove him from ArrayList too.
Here I'am creating the Edittext and adding them in "moreOptList" with there ids.
EditText checkBoxMoreEdt = new EditText(this);
checkBoxMoreEdt.setId(i);
moreOptList.add(i, checkBoxMoreEdt);
Here Iam deleting the view on Button clickListener,but instead of deleting the the exact value from arrayList also its only delete the view and the last index of array from the list.For example if I have enter 4 options
a
b
c
d
and then deleted the option 2 which is b and click ok,it will show me
a
b
c
what its doing is removing the last save value from arrayList now How should I get the Id of exact edittext I want to delete and delete it from arrayList also.
final Button button = new Button(this);
button.setId(b);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chkbParentLayout.removeView(ll);
moreOptList.remove(i);
}
});
Most probably the problem is in ArrayList. When you remove element by index all elements are shifted, hence indexes do not match your views:
list.add(0, 0) //[0]
list.add(1, 1) //[0, 1]
list.remove(0) //[1]
list.remove(1) //throws IndexOutOfBoundsException
One options is to replace ArrayList with HashMap:
map = HashMap<Integer, View>()
map.put(i, view)
...
group.remove(map.remove(i))
Another useful thing is to use setTag/getTag methods of View, you can store your indexes there, so you might not need to have external indexes storage:
view.setTag(index)
index = view.getTag()

Android checkbox array

I have 12 CheckBox in an array and I have to check if more than 1 CheckBox is checked I have to get toast message "cannot select more than 1 checkbox".
My code:
chkArray = new CheckBox[12];
chkArray[0]=(CheckBox) findViewById(R.id.checkBox1);
chkArray[1]=(CheckBox) findViewById(R.id.checkBox2);
chkArray[2]=(CheckBox) findViewById(R.id.checkBox3);
chkArray[3]=(CheckBox) findViewById(R.id.checkBox4);
chkArray[4]=(CheckBox) findViewById(R.id.checkBox5);
and so on
chkArray[0].setOnClickListener(mListener);
chkArray[1].setOnClickListener(mListener);
chkArray[2].setOnClickListener(mListener);
A simple solution is adding a loop in the onCheckedChanged listener where you check each checkbox for true or false. If more than one checkbox is checked (including the last checked checkbox), set that checkbox to false and display the message.
try this
http://developer.android.com/reference/android/util/SparseBooleanArray.html
Store array of boolean value with index as a key.
put this on click listener of checkbox
if (mSparseBooleanArray.get(i)) {
//here value with true state
//here you can know any one item is selected
Toast.makeText(getContext(),"cannot select more than 1 checkbox",
Toast.LENGTH_LONG).show();
return;
}
I'm not sure what you are trying to accomplish, but it sounds like you want the functionality of radiobuttons instead of checkboxes.
RadioButton:
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive.
More information at http://developer.android.com/guide/topics/ui/controls/radiobutton.html

How to check if none of checkboxes is selected in android?

I am working on android project. I have 3 pages in which each page contains some decription and below the desciption 5 to 8 checkboxes. I kept the next button at the top of each page. I am storing the selected check box id's in an arraylist. i.e if I check the checkbox the id will be stored in the arraylist and if I uncheck the checkbox the id will be deleted from the arraylist. (I need the selected checkboxes fo further pages and hence this task). If none of the checkbox is selected and next button is clicked i want to display an alert saying that "Please select atleast one". How to check if none of the checkbox is selected and how to display an alert box? Please help me with this issue.
Thanks in advance
Loop through your ArrayList of IDs and check if each box is checked.
boolean isChecked = false;
for (int id : yourArrayOfIds) {
if (((CheckBox) findViewById(id)).isChecked()) {
isChecked = true;
break;
}
}
// If isChecked==true, a box is checked.
You may also want to check if the CheckBox object is null before using isChecked(). This is just a rudimentary example.
To show your alert, use something like this. There are lots of other tutorials and documentation as well.

How to update non-bound child view for ALL items in a ListView?

I have a ListView that uses a custom layout that has 5 child View objects (4 TextView and 1 CheckBox) in the custom layout. Only the TextViews are bound, however - the CheckBox has no corresponding field in the database. The purpose of the CheckBox is simply to identify which of the items being displayed I would like to process in "the next step". I'm using a custom ViewBinder to assign the text values correctly (because some of the values from the DB are dates, etc).
Part of the user interface is three buttons - 'All', 'None', and 'Invert' that I use to toggle the status of each item in the list. For the 'All' button, for example, I do this with the following code (which I now know is NOT correct - I include it to show what I'm trying to do):
ListAdapter la = getListAdapter();
ListView lv = getListView();
int iCount = la.getCount();
for(int i=0; i<iCount; i++)
{
View vv = lv.getChildAt(i);
CheckBox cb = (CheckBox) vv.findViewById(R.id.ledgerItemCheckBox);
cb.setChecked(true);
}
This works fine as long as the number of items in the list doesn't exceed the list size so that all items are always visible. When I exceed that number, though, the 'getChildAt' function sometimes returns null because (if I understand correctly) it isn't meant to return all of the items in the list, only those items that are visible, which results in a NullPointerException.
How can I properly set the CheckBox on all views, even if they aren't visible?
Create a pseudo binding to one of your TextView data, create an ArrayList of Boolean objects as your back end for the checkboxes. Please note an ArrayList may not be the best data structure for your application. Fill the ArrayList with booleans set to the initial value of the corresponding position of your Cursor's data set. When you're binding use the ArrayList as the back end data. When you select all, none, or invert, update the Boolean objects in the ArrayList then call notifyDataSetChanged() on your SimpleCursorAdapter.
Addition ah, the other half of the problem, yes, use the onClickListener for the CheckBoxes but use the same listener, don't create a new object for each. Use the getPositionForView() method to get your position and update the underlying data.

How to uncheck the CheckBox, when limit exceeds in android?

Hello I am New to Android, My requirement is allow the user to check maximum 3 number of checkboxes. when user is trying to check fourth one should populate dailogue.
Iam trying but the problem is fourth one is not unchecking...
can any one help me...
Regards
Shiva.M
Just uncheck the checkbox using the following code block:
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}

Categories

Resources