how to use the progressBar.getProgressBar to set color in a xml dynamic?

62 views Asked by At

I have a recyclerview and I need to modify the color of a drawable inside OnBindViewHolder, but I can't access the ID of my XML file for that.

I need to access the item with id circler_center and set a different color

My xml

`

<item
    android:id="@+id/circle_center"
    android:gravity="center">
    <shape android:shape="oval">
        <size android:width="58dp"
            android:height="58dp"></size>
        <solid android:color="#E2D5D5"></solid>

    </shape>

</item>

<item>
    <shape android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false">

        <solid android:color="@color/white"></solid>

    </shape>

</item>

<item>
    <shape android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="true">

        <solid android:color="#ffff00"></solid>

    </shape>
</item>

`

My onBindViewHolder

´    @Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {


        Nivels_of_Screen_Middle nivels = list.get(position).get(0);
        MyviewHolder Holder = (MyviewHolder)holder;

        Holder.progressBar.getProgressDrawable()
        .setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);

        Holder.progressBar.setProgress(nivels.getProgressBar());

        Holder.textView.setText(nivels.getTextView());

        Holder.imageView.setImageResource(nivels.getImageView());

} ´

I tried to do the following, used the holder.progressbar.getprogressDrawable (). Setcolorfilter (color.black, porterduff.mode.src_atop); However, the entire progress bar was painted black and not just the item with the ID circler_center

1

There are 1 answers

4
Ruslan On

Option 1

It looks like item with id circle_center can have only two states: 1)it is filled with color E2D5D5 2)it is filled with Color.BLACK. Is this assumption correct? If so, you can create two separate layer-list drawables, which will differ by circle_center item implementation. And you just swap them dynamically.

Option 2

LayerDrawable layerDrawable = (LayerDrawable) context.getResources().getDrawable(R.drawable.yourLayerListDrawableId);
Drawable circleDrawable = (Drawable) layerDrawable.findDrawableByLayerId(R.id.circle_center);