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

14 Responses to “Android: Checkable Linear Layout”

  1. Posted by Androidaler

    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.

  2. Posted by Androidaler

    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 *

  3. Posted by Androidaler

    * 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.

  4. Posted by thk

    Strange, could that be a problem with the adapter that you are using? Sounds like you might not be properly inflating your items.

  5. Posted by Androidaler

    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.

  6. Posted by Androidaler

    Auto correct….fail.

  7. Posted by thk

    Indeed lol :)

  8. Posted by Varun

    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?

  9. Posted by Varun

    OMG, commented on a wrong blog entry!..Sorry , my head’s spinning these days.

  10. Posted by thk

    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

  11. Posted by Eik

    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!

  12. Posted by thk

    You are welcome!

  13. Posted by Fabrizio Giudici

    Thanks from me too. You solved me a few headaches.

  14. Posted by PhiBo

    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. ;)

Leave a Reply