4

Good day to all.

I have a TableLayout with three TextViews in each of its rows. Is it still possible to add OnClickListener to the whole row? I'd like to change the background colour of the selected row. I've set an OnClickListener to the TableRow by doing the following, but the background colour doesn't change:

   for(int i =0; i < rowAmount; i++)
        {           
            TableRow tr= new TableRow(this);

            TextView rmNo;
            TextView s;
            TextView p;

            rmNo = new TextView(this);
            s = new TextView(this);
            p = new TextView(this);

            rmNo.setText("" + roomNumbers.get(i).toString());
            s.setText("" + statuses.get(i).toString());
            p.setText("" + priorities.get(i).toString());

            tr.addView(rmNo);
            tr.addView(s);
            tr.addView(p);      

            tr.setOnClickListener(new View.OnClickListener() 
            {                   
                @Override
                public void onClick(View v)
                {
                    tr.setBackgroundColor(color.holo_blue_light);
                }
            });

            tblContent.addView(tr);
        }

    }

I am creating the TableRows and TextViews programmaticaly because their data is retrieved from a database.

This is the XML:

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tblTitles">   
    <TableLayout 
        android:id="@+id/tblContent"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/ob">


   </TableLayout>
</ScrollView> 

Any help / ideas would be greatly appreciated.

Already looked-up sources:

How can I highlight the table row on click ?

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

Community
  • 1
  • 1
ClaireG
  • 1,244
  • 2
  • 11
  • 23

4 Answers4

8

i tested it, and now it works just fine, try using

tr.setOnClickListener(new View.OnClickListener() 
{                   
    @Override
    public void onClick(View v)
    {
        v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
    }
});

instead...

malimo
  • 770
  • 6
  • 7
  • Hi malimo, no it did not work. I think I need to add a new XML defining the specific row's selected / focusable state, however I find it difficult to do so since I need to not just create a fixed number of table rows. – ClaireG Jul 30 '13 at 09:56
  • i tested it and it works just fine, i don't know what's wrong with your code. but if you want to draw something like a scrollable tablelayout i would suggest using a listview with a listadapter... putting a tablelayout into a scrollview is not best practise. – malimo Jul 30 '13 at 11:13
  • Thank you for pointing that out. I managed to solve it following your example :) – ClaireG Jul 30 '13 at 12:18
1

shape.xml

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

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="#DDA0DD"
                android:endColor="#ffff00"
                android:angle="270" />
                       <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>

    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#ff00ff"
                android:startColor="#000fff"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="#f0f0f0" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="#000000"
                android:startColor="#000000"
                android:angle="270" />
            <stroke
                android:width="1dp"

                android:color="#000000" />
            <corners
                android:radius="3dp" 
                />
            <padding
                android:left="1dp"
                android:top="1dp"
                android:right="1dp"
                android:bottom="10dp" />
        </shape>

    </item>

</selector>

Then add

 tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
selva_pollachi
  • 4,147
  • 4
  • 29
  • 42
1

u can use tr.setOnClickListener(this);

instead of

tr.setOnClickListener(new View.OnClickListener() 
            {                   
                @Override
                public void onClick(View v)
                {
                    tr.setBackgroundColor(color.holo_blue_light);
                }
            });

then

 @Override
                public void onClick(View v)
                {
//you can set background color of 'v'
v.setBackgroundColor(color.holo_blue_light);
                }
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

Which bulid version are u using

try this

tr.setBackgroundColor(Color.BLACK);

or

tr.setBackgroundColor(Color.WHITE);
Ajay nath
  • 133
  • 2
  • 10