19

As my project requirement i have to highlight the table row on onClick. There is any way to do this? Or please suggest me the alternative?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Ravi
  • 211
  • 1
  • 2
  • 4
  • 1
    A bit late but check the answer of this thread: http://stackoverflow.com/questions/6274343/how-to-change-the-background-color-of-a-tablerow-when-focused – Arnaud Jul 21 '11 at 23:49
  • You may refer to [this link](http://stackoverflow.com/questions/4410420/onclick-change-tablerow-background-color) refer to the answer given by Josh Clemm. – prateek Apr 07 '12 at 10:02
  • please see the edited answer here:http://stackoverflow.com/a/7022137/2469134 – Hesham Yassin Mar 12 '17 at 20:02

6 Answers6

27

If you want to use the stock on click highlight like you get with a generic ListView, you want to set the background of each row to be android:background="@android:drawable/list_selector_background"

Here is an example:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0">
  <TableRow
     android:id="@+id/first_row"
     android:background="@android:drawable/list_selector_background" >
    ... row content ...
  </TableRow>
</TableLayout>

Then in code,

TableRow firstRow = (TableRow) findViewById(R.id.first_row);
firstRow.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            // TODO: do your logic here

        }   
}

And you should get a highlight-able row just like in a ListView...

EDIT: Above will give you the default theme's list background selector. If you want the more generic selector (like the material design selector when the user touches a row) use this:

android:background="?android:attr/selectableItemBackground"

Also this applies to more than just TableRows. You should be able to do this on almost any generic widget with an onClickListener attached (TextViews, Buttons, etc).

Salil Pandit
  • 1,498
  • 10
  • 13
  • You obviously haven't tested this. All this does is set the background color of the table row and adds a click-listener to the row, **nothing** more. – arkon Feb 10 '12 at 06:49
  • 6
    I have tested it - and its being used in production rather successfully. You are right it DOES set the background. What it sets it to is the key - the `@android:drawable/list_selector_background` is the phone's theme's standard highlight when you click on an entry in a ListView. Curiously, before dropping a comment, did YOU test it? – Salil Pandit Feb 10 '12 at 18:10
  • I've tested it and it works. It's exactly what I were looking for: button behavior for a TableRow. – Lekensteyn Apr 05 '12 at 19:17
  • 2
    can you pick or change the default colour of the highlight? – topwik Aug 27 '12 at 17:48
  • I wonder how risky it is to refer to android layouts in code? Might those not change/be different on different android versions? – AgentKnopf Jan 13 '13 at 09:57
  • 3
    This behaves somewhat differently than the ListView behavior on my `Nexus 5`. I am seeing an orange background color as apposed to my ListView's light blue. Also, the ListView seems to trickle down the color change to nested elements. – theblang Jun 12 '14 at 20:31
  • This is also not working for me. Nexus 5 running lollipop. I am using appcompat. I get a yellow background on click and it goes away immediately. Possible update for newer OS versions? – lostintranslation May 27 '15 at 02:58
11

Even I was facing the same problem with the help of salil pandit answer made a little change to it and that works for me

This is TableRow in xml:

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="5dip" 
        android:background="@drawable/selector">

This is selector.xml in res\drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item   android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@android:drawable/list_selector_background"></item>
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@android:drawable/list_selector_background"></item>
    <item
            android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@android:drawable/list_selector_background" />


     <item android:drawable="@android:drawable/list_selector_background"></item>

</selector>
Zombie
  • 1,965
  • 2
  • 20
  • 34
9

Within the onclicklistener add:

 tr1.setBackgroundResource(drawable.list_selector_background);

Where tr1 is your tablerow. (you will need to make the tablerow final for it to work).

Denis
  • 1,201
  • 2
  • 10
  • 11
3
private OnClickListener tablerowOnClickListener = new OnClickListener()
{
    public void onClick(View v)
    {
        //Highlight selected row
        //Highlight selected row
        //Start from 0 to make sure that the first item will also be looped 
        //through
        for (int i = 0; i < tblItemDetail.getChildCount(); i++)
        {
            View row = tblItemDetail.getChildAt(i); 
            if (row == v)
            {
                row.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));               
            }
            else
            {
                //Change this to your normal background color.
                row.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        }
        //...
    }
};
Bhhruguni
  • 87
  • 1
  • 2
  • 9
0
String _row_selected = null;
boolean _is_selection_even = false;
private TableLayout TL;
TableRow row_data = new TableRow(this);

row_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (_row_selected != null) {

                    if (Integer.parseInt(_row_selected) == TL.indexOfChild(v)) {

                        if (_is_selection_even) {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(0xFF00FF00);
                            _is_selection_even = false;
                        } else {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                            _is_selection_even = true;
                        }


                    } else {
                        TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                        v.setBackgroundColor(0xFF00FF00);
                        _row_selected = null;
                        _row_selected = TL.indexOfChild(v) + "";
                    }

                } else {
                    v.setBackgroundColor(0xFF00FF00);
                    _row_selected = null;
                    _row_selected = summaryTL.indexOfChild(v) + "";
                }
              }
        });
Bhaskar Vaddadi
  • 758
  • 4
  • 4
0

@SalilPandit 's programmitical version with row selector:

final TableRow row = new TableRow(this);

row.setBackgroundResource(android.R.drawable.list_selector_background);

row.setOnClickListener(new View.OnClickListener(){            

     @Override
     public void onClick(View view){
         row.setFocusable(true);
         // TODO
     }
});

For generic selector ( material design ):

TypedValue typeValue = new TypedValue();
this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                                 typeValue, true);

final TableRow row = new TableRow(this);
row.setBackgroundResource(typeValue.resourceId);

row.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view){
          row.setFocusable(true);
          // TODO
    }
});
Ahtisham
  • 9,170
  • 4
  • 43
  • 57