I have a ListView
and I want it so that when I am pressing on an item, it will change the color to dark gray. I have already tried setSelector()
, but it keeps background color of the item even after the item is released(not being pressed anymore). I would really like to keep the selection animation on lollipop, so I would rather not use xml as you can't target just under lollipop like you can programmatically with Build.VERSION.SDK_INT
. Is there any way to change the background color of an item only while it is being pressed programmatically or is there a way to make xml selector drawables target only certain api levels?
Asked
Active
Viewed 359 times
2

Dylan Vander Berg
- 1,809
- 1
- 22
- 37
-
1Hi, what type of Adapter do you use? SimpleAdapter, BaseAdapter?!? – Dennis Weidmann Aug 24 '15 at 18:47
-
1@Neo I am using a custom adapter extending BaseAdapter – Dylan Vander Berg Aug 24 '15 at 18:49
-
2Ok, I maybe have found a solution in another Question, please Check if this helps you? http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android?rq=1 – Dennis Weidmann Aug 24 '15 at 18:50
-
1If not, write me again, than I will write you a solution that works for your case – Dennis Weidmann Aug 24 '15 at 18:51
-
1It pointed me in the right direction to get it solved. I declared the selector xml just like everyone else that asked this question, but instead of setting it as the `listSelector`, I set it as the background resource in the `getView` using `if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) convertView.setBackgroundResource(R.drawable.list_item_background);`. This also allowed me to keep the same animation for lollipop devices. – Dylan Vander Berg Aug 24 '15 at 19:14
-
Ok, so everything is clear now? Or any other questions? – Dennis Weidmann Aug 24 '15 at 19:16
-
Yup. Everything is working now. Thanks for the help. – Dylan Vander Berg Aug 24 '15 at 19:17