Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I highlight the table row on click ?

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?

like image 550
Ravi Avatar asked Nov 02 '10 06:11

Ravi


People also ask

How do I highlight a row in a table?

To select an entire table, click in the table, and then click the Table Move Handle in the upper-left corner. To select a row, column, cell, or group of cells, click and drag your mouse pointer to highlight the cells you want.

How do I highlight a row in a table in HTML?

When you create Web pages in HTML, you can use JavaScript functions to alter the appearance of page elements on user interaction. To highlight certain rows in a table, you can set Cascading Style Sheet declarations for these rows in their normal state and in their highlighted state.

How do I make a row in a table selectable?

To make the full row selectable, you should add the link to the <tr> instead of to individual table cells. Since you're not allowed to place an <a> tag around a table row, you'll have to think a little bit outside the box to solve this puzzle.

How do you highlight a row in JavaScript?

In order to select the rows based on a data externally using a button, you can use the selectRows method within the click event of the button.


3 Answers

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

like image 141
Salil Pandit Avatar answered Oct 15 '22 18:10

Salil Pandit


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>
like image 31
Zombie Avatar answered Oct 15 '22 16:10

Zombie


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

like image 44
Denis Avatar answered Oct 15 '22 16:10

Denis