Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting onClickListener to TableRow with TextViews

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?

like image 396
ClaireG Avatar asked Jan 12 '23 19:01

ClaireG


2 Answers

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...

like image 73
malimo Avatar answered Jan 16 '23 01:01

malimo


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));
like image 39
selva_pollachi Avatar answered Jan 16 '23 01:01

selva_pollachi