Question

I have a table row having multiple data. When a row is clicked, that particular row is highlighted with dark gray color. But when i click the next row, the clicked row is highlighted but the previous row is still highlighted. How do i disable the highlight of previous row. I tried many tricks, but that's not working.

This is my sample code.

tableRow.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
           v.setBackgroundColor(Color.DKGRAY);

}

I have created the layout programmatically. I am not using XML.

Thanks in advance.

Was it helpful?

Solution

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="@drawable/selector"

Here is an example:

<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="@color/blue></item>
<item   android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@color/custom"></item>
<item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@color/gray" />


 <item android:drawable="@color/white"></item>

</selector>

Update: Create StateListDrawable programmatically like below and set as Background to your TableRow:

 Drawable d1=activity.getResources().getDrawable(R.drawable.gradient_bg_hover);

  GradientDrawable g = new GradientDrawable(Orientation.TOP_BOTTOM,
            new int[] { Color.DKGRAY});       
 g.setGradientType(GradientDrawable.LINEAR_GRADIENT);

            StateListDrawable states = new StateListDrawable();
            states.addState(new int[] {android.R.attr.state_pressed,-android.R.attr.state_selected},d1);
            states.addState(new int[] {-android.R.attr.state_focused},g);

            table_row.setBackgroundDrawable(states);

This is gradient_bg_hover.xml in res\drawable folder .

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient BgColor for listrow Selected -->
<gradient
  android:startColor="#18d7e5"
  android:centerColor="#16cedb"
  android:endColor="#09adb9"
  android:angle="270" />
</shape>

Update2: You can add More State to StateListDrawable as per your requirement.

  1. android:state_activated: set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.

  2. android:state_active: State value for StateListDrawable.

  3. android:state_enabled: Set when a view is enabled.

  4. android:state_focused: State value for StateListDrawable, set when a view has input focus.

  5. android:state_pressed: set when the user is pressing down in a view.

  6. android:state_selected: set when a view (or one of its parents) is currently selected.

More information about StateListDrawable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top