I have a listview that is dynamically populated with CheckedTextViews. The listview is set to multiple choice mode. I'm using OnItemClickListener to respond to clicks on my listview. Also I made an XML file with CheckedTextView's layout (actually it is just a copy of standard android.R.layout.simple_list_item_multiple_choice). So in this case all works fine: when I'm clicking an item in the listview, the appropriate checkedtextview becomes checked. But when I'm trying to use the following layout, the checkedtextview doesn't respond to the click and still unchecked.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:padding="5px">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:drawableLeft="@drawable/checkbox_selector"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:background="#00ffffff"
android:textSize="15sp"
android:singleLine="true"/>
</LinearLayout>
I guess that it because CheckedTextViews are put into LinearLayout and they don't receive the click event from list items of the Listview.
There are two easy solution for this problem:
Remove the LinearLayout and user only CheckedTextView (Yes, it is possible). So the main element of the layout is checkable and it will be marked.
If your minSDK is 11 of above, customize the android:checkMarke and set a state to Activated. Here is an example:
Example code:
Source: http://www.jiahaoliuliu.com/2013/08/customizing-checkbox-in-android.html
The middle level solution is customize the onItemClickListener of the list view, as shown on this code: https://github.com/jiahaoliuliu/CustomizedListRow
The hard but the correct solution is the one proposed by MarvinLab: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
Related issue: What is the difference between the states selected, checked and activated in Android?