Reputation: 3201
I am working on an android application. I have created a listview by using
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayname));
getListView().setTextFilterEnabled(true);
Now i want to change the selected item's color. I could change the background of the selected item by placing
listviewobject.getChildAt(position).setBackgroundColor(Color.BLACK);
in onListItemClick()
this code is changing the background color but if I select any other list item then also the previously clicked list item's color is red.So I change the previously clicked listitem's color by
l.getChildAt(prevpos).setBackgroundColor(Color.BLACK);
now the problem is if i change the background of previously clicked listitems color to black.Then i can't see the text on that particular listitem.I i click again then only i can see the text on that item.So its look weired.please help me friends
Upvotes: 1
Views: 29150
Reputation: 1241
After going through lots of posts and blogs i found this solution it works for me...
declare row variable global
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Upvotes: 19
Reputation: 9020
Well using this in the properties of your list view mught help:
android:listSelector="@drawable/list_selector"
It will automatically set the selected drawable as background of the selected item. Hope this works for you and drawable may be your own choice's color.
Explanation: Add an image strip to your drawables folder/s and set that image in listSelector attribute of the your list view. Now you will navigate through your list view, the list view's background will be of the color of the image strip you set instead of android's native color. Hope you get it now...:-)
Upvotes: 5
Reputation: 1668
By default the background is black. If you have customized your listview then when you scroll it would turn black. So when you define your list view set background cache color to the color you need as below:
yourlistView.setCacheColorHint(Color.WHITE);
Upvotes: 1
Reputation: 4460
I this This happening because you have put text color as black and your setting the background color also black that's why you can't see the difference. for setting the background color you can use the following line.
view.setBackgroundColor(Color.YELLOW);
use different color then text color.
Upvotes: 1
Reputation: 1179
What you can do is instead of using .setBackgroundColor()
with a Color value, create a Color State List and assign it with .setBackgroundResource()
. That way, you can define the various states that your list item can become, depending on the item's current state.
Upvotes: 0