Reputation: 24583
I have this drawable:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/circle">
<shape android:shape="oval">
<size android:width="120dp" android:height="120dp"></size>
<stroke android:width="4dp"/>
</shape>
</item>
<item
android:id="@+id/person"
android:top="16dp"
android:bottom="16dp"
android:left="16dp"
android:right="16dp">
<bitmap android:src="@drawable/ic_person_48dp" />
</item>
</layer-list>
I am setting this as an item in a recycler view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:clickable="true"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/person_icon"/>
</LinearLayout>
I want to set a different color for each cell in the bind method:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageView imageView = holder.personImageView;
// How do I grab the circle shape and person bitmap from the image view
// and set the colors for this cell?
}
I need this to work from api 17 and up.
Upvotes: 0
Views: 1544
Reputation:
Try this:
final LayerDrawable layerDrawable = holder.personImageView.getDrawable().mutate();
layerDrawable.getDrawable(index).mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
You have to call the drawable's mutate()
method, else the color will change for every iteration of the LayerDrawable.
Upvotes: 5