I have a ListView with a bunch of ListItem’s. When the user selects an item, I would like to change that ListItem’s background to an image. How can I accomplish this?
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// how do I change the ListItem background here?
}
});
You can set that in the adapter of your
ListView. What you have to do is use thesetBackgroundResourceon theViewthat you are returning. Let me give you a brief example:Notice that I’m using
R.drawable.the_custom_background, which means that you have to write a little XML selector. Don’t worry, it’s easier than it sounds. You create an XML file calledthe_custom_background.xmlinside theres/drawablefolder:Notice again, I’m using
@drawable/the_background_color, so finally create another drawable in theres/drawablefolder calledthe_background_color:I know it seems to be very messy, but this is the Android way. You could also try to modify the
Viewinside thesetOnItemClickListener, but I think that’s undesirable and harder to implement.