StateListDrawable not working on TableRow

577 Views Asked by At

I'm trying to get a TableRow, in a TableLayout, to change the background color when a user touches it, but it doesn't seem to be working. When I click on the TableRow, nothing happens. This what I have so far:

Layout:

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  style="@style/BaseStyle"
   android:stretchColumns="*">

      <TableRow style="@style/TableRow">   
           <TextView 
                 android:id="@+id/settings
                 style="@style/BaseStyle.SettingsText"
                 android:text="Settings"
                 android:onClick="onClick"
                 android:clickable="true"
            />  
      </TableRow>

 </TableLayout>

Style:

<style name="TableRow">
     <item name="android:background">@drawable/onclickhighlight</item>
</style>

Drawable (onclickhighlight.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

     <!--  Active state --> 
     <item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/background_light" />

      <!--  Inactive state-->
     <item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" />

      <!--  Pressed state-->
      <item android:state_selected="true" android:state_focused="true" android:state_pressed="true" android:drawable="@android:color/background_light" />

</selector>

Colors:

 <resources>
     <color name="background_light">#FFFFFF</color>
 </resources>

I know the TableRow is using the onclickhighlight StateListDrawable because if I change the "Inactive state"s android:drawable property from @android:color/transparent, to @android:color/background_light, the background color changes to white.

2

There are 2 best solutions below

1
On

I am having exactly the same issue and I'm trying to find the answer. I bet if you were to remove the android:onClick="onClick" from the table row xml, you'd find the highlight would work. But then, how would you get the click event to associate with the method you want?!

0
On

I had the same problem, and this is what worked for me.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_pressed="true" android:drawable="@color/yourcolor" />
    <item android:drawable="@color/yournormalbackground"/> 
</selector>