3

I have a shape drawable in the drawable folder. Here is XML structure:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient 
        android:startColor="#4D0418" 
        android:centerColor="#5F031D"
        android:endColor="#7D0227" 
        android:angle="-90" />
</shape>  

Now I want to change the startColor, centerColor, endColor from the java code in the runtime. How it is possible to change?

dev_android
  • 8,698
  • 22
  • 91
  • 148

1 Answers1

12

Unfortunately there's no API to do this. I'll add one to a future Android release.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 1
    Is there anyway to recreate the total drawable through java code without using any XML? – dev_android Oct 28 '11 at 08:48
  • I have created a GradientDrawable which is substitute of . But how can I set it to any shape? Should I use ShapeDrawable for that? if it is, then how I can set my GradientDrawable to that ShapeDrawable? – dev_android Oct 28 '11 at 12:32
  • 2
    GradientDrawable is a substitute for both and – Romain Guy Oct 28 '11 at 19:27
  • GradientDrawable doesn't appear to have anything equivalent to the ShapeDrawable's setShaderFactory method, [which can be used to create a gradient with more than just 3 colors](http://stackoverflow.com/questions/4381033/multi-gradient-shapes). Hmm. :-/ – Dr. Wily's Apprentice Jan 20 '12 at 20:12
  • This appears to be in [API 16](http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setColors(int[])). – yincrash Jun 11 '14 at 21:01