<!--Even row -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/selected" />
<item android:state_selected="true" android:drawable="@color/selected" />
<item android:state_focused="true" android:drawable="@color/selected" />
<item android:drawable="@color/even_row" />
<item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@color/even_row" />
</selector>
<?xml version="1.0" encoding="utf-8"?><!--Odd row -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/selected" />
<item android:state_selected="true" android:drawable="@color/selected" />
<item android:state_focused="true" android:drawable="@color/selected" />
<item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>java code:
for (String data:rowData){ TextView col = new TextView(context); col.setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER); col.setText(data); col.setTextSize(24); if (clickable){ if (table.getChildCount()%2==0) col.setBackgroundResource(R.drawable.table_selector_odd); else col.setBackgroundResource(R.drawable.table_selector_even); } row.addView(col, col1Params); }