Reputation: 4698
i have tried many example but result is same,My requirement is when a list item is clicked then i want to show that item as selected by a color.and that item must be show selected until some other list item is clicked.
tried many example but result is - when i click on a list item its color change while it pressed after release its color change to default color.
i want to achieve some thing like see here but this code is also now working for me.
So how can i achieve this type of view.
Upvotes: 0
Views: 137
Reputation: 1065
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
for(int i=0;i<parent.getChildCount();i++)
{
parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
v.setBackgroundColor(Color.BLUE);
}
});
Upvotes: 1
Reputation: 2621
Create a new xml in the drawable folder name it whatever you want.
<?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/white" />
<item
android:state_selected="true"
android:drawable="@drawable/list_item_bg_selected" />
<item
android:drawable="@color/list_bg" />
</selector>
And set it as a background for your listview.
Upvotes: 0