0

I have a custom view which extends TextView and the background is a drawable,

<CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circile_background" />

and want to change the background color in custom view

public class CustomView extends TextView {

    TypedArray typedArray;

    public CustomView (Context context) {
        super(context);
    }

    public CustomView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void setColorLevel(int color) {
       // update color here
    }
}
A-Sharabiani
  • 15,608
  • 15
  • 99
  • 122
Xianwei
  • 2,103
  • 2
  • 20
  • 26
  • Do you want to keep both background image and color...? – Dhruv Nov 14 '17 at 09:05
  • Thanks . Yes, I want keep the image and just change the imagecolor – Xianwei Nov 14 '17 at 09:17
  • 1
    `getBackground().setColorFilter(color, PorterDuff.Mode.SRC);` – Mike M. Nov 14 '17 at 09:22
  • 1
    @MikeM Thank so much it works. – Xianwei Nov 14 '17 at 09:33
  • No problem. It looks like svkaka posted an answer below with the same idea. If it helped, you might consider accepting it. Feel free to ask them to edit it a bit, if maybe `MULTIPLY` isn't the exact effect you're looking for. Glad you got it working. Cheers! – Mike M. Nov 14 '17 at 09:44

1 Answers1

1

check similar question here How to change colors of a Drawable in Android?

public void setDangerLevel(int color) {
    Drawable drawable=this.getBackground().getCurrent();
    drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    }
svkaka
  • 3,682
  • 29
  • 50