Home » Android » android – GradientDrawable change angle programmatically

android – GradientDrawable change angle programmatically

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have following xml for GradientDrawable. How can I change the angle programmatically?

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:type="linear" 
       android:angle="45" 
       android:startColor="#FF0000" 
       android:endColor="#00FF00" />
</shape>
How to&Answers:

GradientDrawable has a method called: .setOrientation(GradientDrawable.Orientation orientation), you can change the orientation of a gradient with this. You need to inflate the XML as GradientDrawable.

Answer:

Try:

GradientDrawable appTheme = new GradientDrawable(GradientDrawable.Orientation.BL_TR, colors);