I’m creating a ListView with buttons and having some issues.
My activity need to do 2 different actions for each action (ItemClick and buttonClick).
I assumed that:
1 – Because I has button on list items, I cant use OnItemClickListener(). Right?
So, I create layout for list items and make it clickable.
listitem_textview_button.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="@drawable/selector_list_item">
<Button
android:id="@+id/listitem_textview_button_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:text="@string/edit" />
<TextView
android:id="@+id/listitem_textview_button_txv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="14sp"
android:textColor="@drawable/selector_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_toRightOf="@+id/listitem_single_line_w_button_btn" />
</RelativeLayout>
Notice that I’ve created a selector for the layout and a stateColorList for Textview.
selector_list_item.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="@color/transparent" />
<item
android:state_focused="true"
android:state_enabled="false"
android:state_pressed="true"
android:drawable="@drawable/shape_list_item_disabled" />
<item
android:state_focused="true"
android:state_enabled="false"
android:drawable="@drawable/shape_list_item_disabled" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/shape_list_item_transition" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/shape_list_item_transition" />
<item
android:state_focused="true"
android:drawable="@drawable/shape_list_item_focus" />
</selector>
selector_textview.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/red" />
</selector>
2 – This is the best way to implement a ListView with custom items (included TextView Colors)?
The above code does not change the color of textViews when I click on items.
In some tests I saw that the text color changes when:
1. Using arrows of emulator.
2. Removing the button of ListView item.
Where is the problem?
prinscreens:
listitem selected by arrow device (text black, ok!)

listitem clicked by finger (text red, should be black, fail)

answer:
add android:duplicateParentState="true" to TextView.
I don’t get it, your layout is clickable but you have buttons? Is the button for selecting a row?
If I were you I would not make anything clickable except the button. android:focusable=”false” android:clickable=”false”
Then you can in the button listener set the selection on a row manually:
Let me know if this works.
[EDIT] The real problem is that the TextView needs to be clickable, not the layout.