Changing gradient background colors on Android at runtime -
i'm experimenting drawable backgrounds , have had no problems far.
i'm trying change gradient background color @ runtime.
unfortunately, there's no api change @ runtime, seems. not trying mutate() drawable, explained here: drawable mutations
the sample xml looks this. works, expected.
<shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">     <gradient         android:startcolor="#330000ff"         android:endcolor="#110000ff"         android:angle="90"/> </shape>   sadly, want list various colors, , they'd have programatically altered @ runtime.
is there way create gradient background @ runtime? perhaps not using xml altogether?
yes! found way!
had forget xml, here's how did it:
on getview() overloaded function (listadapter) had to:
    int h = v.getheight();     shapedrawable mdrawable = new shapedrawable(new rectshape());     mdrawable.getpaint().setshader(new lineargradient(0, 0, 0, h, color.parsecolor("#330000ff"), color.parsecolor("#110000ff"), shader.tilemode.repeat));     v.setbackgrounddrawable(mdrawable);   and gave me same result xml background above. can programmatically set background color.
Comments
Post a Comment