Rose18
Rose18

Reputation: 3160

Android : Change background color of table rows when it is clicked

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

Upvotes: 2

Views: 3827

Answers (3)

portsample
portsample

Reputation: 2112

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!

Upvotes: 1

Kalai.G
Kalai.G

Reputation: 1610

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?

Upvotes: 1

bourax webmaster
bourax webmaster

Reputation: 733

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

Upvotes: 0

Related Questions