2

I have a TableRow which contains a LinearLayout, and then this LinearLayout contains a TextView. What I want is, once the TextView is clicked, the entire TableRow would change its background color.

I've tried to use getParent() and performClick() to pass the click event from TextView to TableRow. The onClick() method of TableRow does get called, but its background color does not change.

Of course I've set the selector by using either

row.setBackgroundResource(R.drawable.menu_item_bgcolor);

or

row.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.menu_item_bgcolor));

Doesn't work. Can anyone provide any insight into this? Thanks,

Below is the selector xml file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:drawable="@drawable/menu_item_pressed" />
<item android:state_focused="true" android:drawable="@drawable/menu_item_pressed" />
<item android:drawable="@drawable/menu_item_normal" />

</selector>
Jiechao Wang
  • 922
  • 1
  • 15
  • 32

1 Answers1

0

try this

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
    getResources().getDrawable(R.drawable.normal));
row.setImageDrawable(states);
Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
  • I change setImageDrawable() to setBackgroundDrawable() because setImageDrawable() does not exist on TableRow. When I click the TableRow, it will change color. But When I click the TextView, the TableRow does not change color. I still can't get this... – Jiechao Wang Apr 21 '12 at 15:49