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?

1
  • You should be able to do it using Java reflection. Commented Oct 28, 2011 at 8:23

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

5 Comments

Is there anyway to recreate the total drawable through java code without using any XML?
I have created a GradientDrawable which is substitute of <gradient></gradient>. 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?
GradientDrawable is a substitute for both <shape/> and <gradient/>
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. Hmm. :-/
This appears to be in API 16.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.