Setting onClickListener to TableRow with TextViews

16,240

Solution 1

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

Solution 2

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));

Solution 3

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);
                }
Share:
16,240
ClaireG
Author by

ClaireG

Updated on June 04, 2022

Comments

  • ClaireG
    ClaireG almost 2 years

    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?