Reputation: 743
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
And Here is the current state on the device
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.
Upvotes: 0
Views: 2076
Reputation: 1
You should use PorterDuff.Mode.SRC_ATOP insteasd of PorterDuff.Mode.MULTIPLY
Upvotes: 0
Reputation: 7259
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>
Upvotes: 0
Reputation: 39013
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.
Upvotes: 1