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.
android:state_activated:
set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.android:state_active:
State value forStateListDrawable
.android:state_enabled:
Set when a view is enabled.android:state_focused:
State value forStateListDrawable
, set when a view has input focus.android:state_pressed:
set when the user is pressing down in a view.android:state_selected:
set when a view (or one of its parents) is currently selected.
More information about StateListDrawable