I want to change the background of a listview item when the user clicks it. Kind of like the Honeycomb settings page (Although I'm not dealing with just settings so I'm not using PreferenceActivity) I have this functionality working through a resource state selector state selector except for the cases when clicking on the listview menu changes the linear layout to the right of the listview (sort of a split screen view). I'm guessing the listview looses focus so state_pressed is no longer true.
<item android:state_pressed="true">
<shape >
<solid android:color="@color/blue1" />
</shape>
</item>
Any tips to keep that listview item colored until another listview item is selected? Thanks!
EDIT:
I was able to get the background changed in a setOnItemClickListener with
view.setBackgroundResource(R.color.red);
I only need one selected at a time so when the other list items are clicked, I tried lv.invalidate()
and lv.getChildAt(0).invalidate()
but neither worked and the second causes null pointer exception. Any ideas for putting the color back?
When you release your finger from the cell it no longer registers as pressed. What you are going to want to do is actually change the background of the individual row when a users selects is. This means implementing an onItemClick or onItemTouch and flagging the adapter to redraw the row with the new background. If you are already using a custom list adapter you can just implement a check against a boolean in your getView() method. You will also need to keep track which rows are 'selected' and which are not.
pseudocode:
public View getView(int pos, View convertView, ViewGroup parent) {
if(isChecked[pos]) //set background to checked color
}