Setting background programmatically overrides drawable
android, android-drawable, android-layout, android-listview
Solution
Expanding on dum's answer, you don't need to do all that work in code.
Drawable A
<?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/light_blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />
<item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />
<item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>
Drawable B
<?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/light_blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />
<item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />
<item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/gray" />
</selector>
In your Adapter:
if (position % 2 == 0) {
convertView.setBackgroundResource(R.drawable.A);
} else {
convertView.setBackgroundResource(R.drawable.B);
}
Problem
I have a `ListView` and I have a `Drawable` for each item in the `ListView` to highlight each row when it's selected/pressed. I also have a custom adapter where I'm programatically setting the background color of each row (I want to have alternating background colors). This is a new feature I added and before adding the code the rows would highlight blue, but after they do not highlight. Not sure how to fix it. Here is what I have: ListView item layout ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingLeft="5dip" android:paddingRight="5dip" android:paddingTop="8dip" android:paddingBottom="8dip" android:background="@drawable/app_selector" > <TextView android:id="@+id/text" style="@style/ListingTitle" android:layout_alignParentTop="true" android:paddingBottom="2dip" /> </RelativeLayout> ``` Drawable ``` <?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/light_blue" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" /> <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" /> <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> </selector> ``` Fragment ``` public class ListingFragment extends SherlockListFragment { public void onActivityCreated(final Bundle icicle) { ListView lv = getListView(); mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) { } @Override public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) { } } } /* ADAPTER */ private class CustomAdapter extends BaseAdapter { @Override public View getView(final int position, View convertView, final ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.item, parent, false); } else { holder = (ViewHolder)convertView.getTag(); } if (position % 2 == 0) convertView.setBackgroundColor(Color.GRAY); else convertView.setBackgroundColor(Color.TRANSPARENT); return(convertView); } } } ```