A common thing you might want to do, when you developing an Android app, is to have a ListView of checkable items. For instance, you might want to have a functionality in your app to share something with a bunch of people, who you would like to select from a list of contacts. For common choice for this task is to use a CheckedTextView and the android:choiceMode parameter of the ListView. This extension of a regular TextView has a check box on the right, so that it can be selected.
However, there is a limitation here. CheckedTextView has to be the root element of the layout that you use for your items. So if you wish to display multiple things (user name and email) for each item by using a LinearLayout, you are out of luck. The reason for this limitation is that he root item must implement the Checkable interface and the LinearLayout doesn’t implement it.
The solution here is to…
…override LinearLayout to implement the required interface. I present to you CheckableLinearLayout. Upon inflation it searches through its children for the CheckedTextView, and then forwards any calls required by the Checkable interfaces to that component:
Now, we can use that component in the layout file for our list items:
Make sure you set in your ListView’s XML: android:choiceMode=”multipleChoice”.
This class is now a part of my Android library Begemot.
Follow me @tokudu
June 7th, 2010 at 5:16 pm
Thanks for the class example.
However, I’m still running into an issue that I have only seen mentioned a couple times in forums or other threads. And that is that I can only tick (read: check) one checkbox even though I’ve set the choice mode to muliple. And more weird is that if I scroll through my ListView’s files I see checkboxes ticked exactly so many checkboxes apart!
Can you please shed some light on what is happening, how I can fix it? I’ve really tried to solve this myself for about three days now.
Here is the code for my (ListView) row.xml:
Plus, I’ve tried different variations of:
android:focusable=”false”
android:clickable=”true”
android:checked=”false”‘
Thanks for any help you can, may offer.
June 7th, 2010 at 5:18 pm
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/LinearLayout01″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>
ListLayout
* some angle brackets removed to get this to post *
June 7th, 2010 at 5:19 pm
* guess it’s not going to let me paste in my code, I’m sure you get the idea *
It’s Imageview, TextView, TextView, CheckedTextView, from top to bottom wrapped in a LinearLayout.
June 8th, 2010 at 8:54 pm
Strange, could that be a problem with the adapter that you are using? Sounds like you might not be properly inflating your items.
June 9th, 2010 at 12:28 pm
Okay, I’ll double check that, thanks.
I did get it to work but only if the user clicks the list items and not the cheekbones themselves.
June 9th, 2010 at 12:29 pm
Auto correct….fail.
June 9th, 2010 at 2:10 pm
Indeed lol
June 17th, 2010 at 3:17 am
Sir,
The blog entry was extremely helpful to get started with Push notifications.
I spent some time looking into the basic function calls to push messages via the IBM really small broker.
I have a query.How did you make the ‘push messaging’ localized to a specific device id?..The publish function for the client has only four parameters namely
Topic,payload,qos and the retained flag i.e.:
“publish(java.lang.String thisTopic, byte[] thisMessage, int thisQoS, boolean retained)”
Since the source of the IBM broker is not available , how is the default broadcasting of the broker stopped and ability to push messages to a particular device with a specific device id can be implemented?
June 17th, 2010 at 3:20 am
OMG, commented on a wrong blog entry!..Sorry , my head’s spinning these days.
June 18th, 2010 at 11:10 am
Lol, it’s alright. Have a look in my code, but basically I embed the device ID as a part of the message topic. Good luck
August 12th, 2010 at 4:36 pm
Hey i just want to say thank you!!! I searched the whole Web, because i implemented a custom Spinner View and its working, but due to i’m using multiple TextViews in my Spinner DropDown Items i have to use a LinearLayout and guess what, the Checked State doens’t working. But you clearly described the problem and your solution works without any modifications.
Again, thank you and greetings from Germany!
August 16th, 2010 at 10:55 pm
You are welcome!
August 25th, 2010 at 12:13 pm
Thanks from me too. You solved me a few headaches.
September 2nd, 2010 at 2:57 am
Wow, you have no idea of how much time I spent on searching for a solution like this. Thanks a lot. If you had a Flattr button, I’d click it.