Changing gradient background colors on Android at runtime

Yes! Found a way!

Had to forget about XML, but here’s how I did it:

On my getView() overloaded function (ListAdapter) I just 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 that gave me the same result as the XML background above. Now I can programmatically set the background color.

Leave a Comment