CheckedTextView doesn't respond to click

2.8k Views Asked by At

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.

5

There are 5 best solutions below

0
On

There are two easy solution for this problem:

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

  2. If your minSDK is 11 of above, customize the android:checkMarke and set a state to Activated. Here is an example:

Example code:

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>  
   <item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />  
   <item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />  
   <item android:state_checked="true" android:drawable="@drawable/checkbox_selected" /> 
</selector>

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?

0
On

Well even if they don't receive the OnClickEvent you can maintain the state of these checkboxes using a boolean ArrayList of same size of List Items. Here, I have just answrered something like that. I hope it will give you a better idea.

0
On

I guess different. If you look at the source code of simple_list_item_multiple_choice.xml, you'll see an android:checkMark attribute set to ?android:attr/listChoiceIndicatorMultiple. This is what the CheckedTextView internally uses to draw the check box in whatever state it happens to be, as you may be able to see from its source code.

But the definition of CheckedTextView in your layout lacks this attribute. This I would blame, rather than the use of CheckedTextView in a LinearLayout element.

I was wrong and you guessed right. It seems that the row for the custom ListView needs to implement Checkable, which CheckedTextView does but LinearLayout does not. See this answer in another StackOverflow thread for details.

0
On

I had the same problem, so I changed the layout to android.R.layout.simple_list_item_multiple_choice instead of using a custom layout with a CheckedTextView.

0
On

This issue is caused by the attr singleLine = true. If you delete this or replace with maxLines = 1, it will work fine. Actually, the status is checked when you click on the item — I think it's an Android bug.