ngesh
ngesh

Reputation: 13501

How to change the background color of a TableRow when focused?

I have a table row and I tried many things to change its color when it's focused, but it never changes color when focused. Is there any way to do this? I tried this also which changed color on click but only when its' focused:

r.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View arg0, MotionEvent arg1) {
        if(arg0.isFocused())
        {
            arg0.setBackgroundColor(Color.BLUE);
            //arg0.isFocused();
        }
        return false;
}});

Upvotes: 2

Views: 9186

Answers (2)

Adam
Adam

Reputation: 26497

To everyone with the "resource not found" error for yellow, just make your own yellow:

Create a file:

 Project
  -> res
    -> values
      - colors.xml

And put this in the file:

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
   <color name="yellow">#FFFF00</color>
 </resources>

Then change the places in XML that reference yellow to read like this:

android:drawable="@color/yellow"

Upvotes: 4

Stuti
Stuti

Reputation: 1638

You need to set the background color of your row to a state list drawable (that handles select, pressed, active, non-active).

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

    <?xml version="1.0" encoding="utf-8"?> 
<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/transparent" />     
<!--  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_pressed="true" android:drawable="@android:color/yellow" /> 
    <!--  Selected state (using d-pad) -->
     <item android:state_focused="true" android:state_selected="true"         android:state_pressed="false" android:drawable="@android:color/yellow" />
 </selector> 

try these links too, for ur problem

http://www.gersic.com/blog.php?id=56

http://developer.android.com/guide/topics/ui/themes.html

Upvotes: 6

Related Questions