0

I am having a ListView where each item consists of an ImageView and a TextView, when an item is selected I have a selector that sets the background of the whole item to strong blue color.
What I want is that when the list item is selected I want to set the foreground color of an ImageView to white.

Here is what I want to achieve

enter image description here

And Here is the current state on the device

enter image description here

The code of the selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_selected="true"
    android:drawable="@color/strong_blue" />
<item android:drawable="@android:color/transparent" />
</selector>

And here is the code of the OnItemClickListener

void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
    view.setSelected(true);
    ImageView imageView = (ImageView) view.findViewById(R.id.wishListDrawerImageView);
    imageView.setColorFilter(Color.WHITE, android.graphics.PorterDuff.Mode.MULTIPLY );
}

Thanks in Advance.

UPDATE

Thanks guys for the good answers but I've thought about the second version of the image being white in color, but unfortunately that these icons is dynamically assigned, I cannot predict which image is going to be shown in the ListView thus I cannot set the white version of the image.

Ahmed I. Khalil
  • 743
  • 6
  • 20

4 Answers4

1

You can't set the image's "foreground color", as there is no such thing. You will have to switch images. You can change an image's color, as is explained here. I prefer the first solution.

However, a much simpler solution in my opinion is to keep two different images in your resource folder - one in gray and the other in white, and switch between the two.

Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171
  • I tried setting the ColorFilter as the code above, but it was not working on white color although any other color like red for example is working with no problem. But the second answer(the chosen answer) is interesting I'll try it and see the results. – Ahmed I. Khalil Jan 14 '14 at 09:03
0

you can use layer-list drawable from this

Fahriyal Afif
  • 560
  • 3
  • 11
0

set the listSelector to that blue type selector that you want, and on the image view, set the selector that has the white image and set it as the background of the imageview.

The imageviews selector can be written in the same way as that of the listselector just that the drawables would vary.

Hope that helps!

EDIT :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true"
    android:drawable="@drawable/white_image" />
<item android:drawable="@drawable/regular_image" />
</selector>
0

You should use PorterDuff.Mode.SRC_ATOP insteasd of PorterDuff.Mode.MULTIPLY