Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Toggle Check and Unchecked for an Item

+13 votes
Having displayed the checkbox adjacent to each grocery item (using checkedTextView), what functionality needs to be changed to allow the checkbox to toggle between checked and unchecked?  Is this in the code or the properties?
asked Mar 22, 2015 in Spring 2015 by Reed Kottke (100 points)

3 Answers

+6 votes
 
Best answer

I have found a solution to half of the problem so bare with me.

 First in the properties set the checkMark to @android:drawable/checkbox_off_background

Then in your code in the onItemClick method you need to make a variable that references your id for the CheckedTextView.  Then with this variable you can use the .setCheckMarkDrawable() method to switch back and forth between the android.R.drawable.checkbox_off_background and android.R.drawable.checkbox_on_background.

This will switch back and forth between the two and to test these code I made a global boolean value as a means to switch between both checkboxes.  Using the boolean did work but the only problem is that the global boolean doesn't allow for multiple items to be switched correctly, due to the fact that each item has its own "checked" state.  There is a method called .setChecked(boolean) to change between true or false for the checked state of that item.  For some reason I can not get the checked state to switch by calling this method, but hopefully this helps.

answered Mar 22, 2015 by David Devore (100 points)
selected May 20, 2015 by Reed Kottke
+6 votes
There's a CheckedTextView.toggle() method that changes the boolean "checked" variable on/off.  This is handy, but it won't automatically change the *appearance* of the widget.  So you can either do what David suggests, OR you can set your checkMark drawable to be an XML choice (there are some in the System resources) that will display a different drawable, depending on whether the checked state is true or false.  If you do the latter, then Android *will* update the graphical view for you...

(You should also be able to make your own XML/drawable resources and add them to your project, to use them, if you want to customize your app.)

P.S.  Yes, you need to handle this with Java code -- i.e., in the onItemClickListener, when they click on an item in the list view.
answered Mar 22, 2015 by Forrest Stonedahl (100 points)
0 votes

I found a very easy way to toggle the checkbox, however it involves the android:drawableLeft attribute and not the checkMark attribute. 

Set (in list_item.xml or properties) android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" (or drawable right) which will create the checkbox. Then in the java file you can set choice mode for the ListView in the onCreate method using: getListView().setChoiceMode(CHOICE_MODE_MULTIPLE);

And then multiple checkboxes can be checked/unchecked at once! 

answered Mar 22, 2015 by Trevor Warner (100 points)
...