6

I want to change the color of the view under focus; in my case it is a table row.

This is my current code, but it doesn't work.

            r.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1) {
                {               {
                    arg0.setBackgroundColor(Color.LTGRAY);
                    r.setFocusableInTouchMode(true);
                }
            return false;
        }});
whostolemyhat
  • 3,107
  • 4
  • 34
  • 50
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • What actually is happening using this code – ingsaurabh May 20 '11 at 05:46
  • sorry.. look at the edited one.. after doin this the whole row changes color once i click on it.. and on selecting multiple rows evry row sustains that color.. i tried using isFocussed() in if condition.. did't work – ngesh May 20 '11 at 05:54
  • I am not sure about it, but according to me tablerow's color doesn't change on focus. You can use ListView for it – Jaydeep Khamar May 20 '11 at 07:03
  • But r u sure listView can serve the purpose of tablerow.. – ngesh May 20 '11 at 07:11

2 Answers2

4

You can use XML drawable,
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

create a xml like this

<?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/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

and you need background pic for that

Tek Yin
  • 3,011
  • 3
  • 25
  • 42
  • k thanks.. let me see if this works out.. the above xml file should pasted is res directory manually i guess...? – ngesh May 20 '11 at 06:28
  • You should accept the answer if it worked for you or reply what output you get.so that we can also help you in other way if this answer didn't worked:) – Jaydeep Khamar May 20 '11 at 06:42
  • everyone around suggests the same answer.. but its bit difficult proceedure since u need to have clear cut information of file system of android.. its not just coding.. and i'm a fresher and not that perfect.. still working on it.. let u know if i get it.. – ngesh May 20 '11 at 06:45
1

Here I make some example for you... http://life-as-a-coder.com/_shared/background_state.rar

Take a look at my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/item_background"
        android:clickable="true">
        <TextView
            android:text="Item 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dip"></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/item_background"
        android:clickable="true">
        <TextView
            android:text="Item 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dip"></TextView>
    </LinearLayout>
</LinearLayout>

I use LinearLayout for this example. I set android:clickable="true" to activate the click state... This can be used in ListView too... just use it on custom row Item...

Sorry for my bad English n grammar

Tek Yin
  • 3,011
  • 3
  • 25
  • 42