2

In my android application there is a table layout which is loaded at run time. I set an OnClickListener for table rows.

tr.setClickable(true);
tr.setOnClickListener(trOnClickListener);

I need to change the background color of the table row when it is clicked. This is my onClick method.

private OnClickListener trOnClickListener = new OnClickListener() {
    public void onClick(View v) {

        TableRow tablerow = (TableRow)v;

        tablerow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.ab_stacked_solid_whiteaction));

    }
};

When I click a table row it changes the background color. When I click another row it also changes its' background color. But what I want is, should be changed background color of only one row at a time.

How can I do that ? Any suggestions are appreciated.

Thanks in advance

Rose18
  • 2,892
  • 8
  • 47
  • 98

3 Answers3

1

Create XML in drawable folder and set this as bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/cell_shape_in_white" android:state_pressed="true"/>
    <item android:drawable="@drawable/cell_shape_in_grey"/>

</selector>

OR

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


    <item android:drawable="@drawable/cell_shape_in_white" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/cell_shape_in_white" android:state_focused="true"/>
    <item android:drawable="@drawable/cell_shape_in_grey" android:state_focused="false"/>
    <item android:drawable="@drawable/cell_shape_in_grey"/>

</selector>

OR

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

Community
  • 1
  • 1
Kalai.G
  • 1,610
  • 13
  • 22
  • I've used this. Then when I click a row it changes its' background color to yellow(as an example). But in a second it changes back to previous color(white). I need to keep this color(yellow) until user clicks another row. When user clicks another row its' color should be changed to yellow and the color of previously clicked row should be white. Actually, only one row can be yellow. How can I do that? Can you plz explain ? – Rose18 Nov 15 '13 at 08:45
  • Are u creating tablerow dynamically – Kalai.G Nov 15 '13 at 08:53
  • Yes. My table layout is loading dynamically. – Rose18 Nov 15 '13 at 08:54
  • It's varying according to the no of rows in the database. – Rose18 Nov 15 '13 at 09:02
  • I've tried. Then all rows are grey and it's not change the color when it clicks./ – Rose18 Nov 15 '13 at 09:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41245/discussion-between-kalai-g-and-ann18) – Kalai.G Nov 15 '13 at 09:33
1

This non-xml fix causes the previously selected row to revert to the neutral table background color (Lt. grey) when an additional row is selected and highlighted (yellow). This is achieved by referencing the appropriate index numbers of selected (child) rows.

public class dataTable extends Fragment {
TextView tvSelectedRow = null;
...
row.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    if(tvSelectedRow == null) {
                        iRowNumber = tableLayout.indexOfChild(v); //This returns the rowID of the selected row
                        tableLayout.setBackgroundResource(R.color.LtGrey); //sets background color to LtGray for whole table.
                        row.setBackgroundResource(R.color.yellow);//changes background color of selected row to yellow.                           
                    }else if(tvSelectedRow != null){
                        tableLayout.getChildAt(iRowNumber).setBackgroundResource(R.color.LtGrey);//causes previously selected row to revert to LtGray.
                        iRowNumber = tableLayout.indexOfChild(v); //Resets iRowNumber to new row selected by user....                      
                        row.setBackgroundResource(R.color.yellow);//changes background color of selected row to yellow.                           
                    }
                }
            });

Best of luck!

portsample
  • 1,986
  • 4
  • 19
  • 35
0

try this

private View pressedView = null; 

private OnItemClickListener getStartedListItem = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
     Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
     myintent.putExtra("SelectedItem", getStartedItems[position]);
     startActivity(myintent);

     if(pressedView != null) {
         pressedView.setBackgroundResource(..); // reset background of old item
         pressedView = arg1; // Point pressedView to new item
     }
}
};
bourax webmaster
  • 748
  • 7
  • 18