question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Programmatically Edit Colors in the Gradient of a GradientDrawable

I have the following drawable which I use as the background for a TextView:

 <?xml version="1.0" encoding="utf-8" ?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item><shape android:shape="rectangle"><gradient android:centerX="0.5" android:centerY="0.5" android:centerColor="@color/White" android:startColor="@color/Red" android:endColor="@color/Red"/></shape></item>
     <item><shape android:shape="rectangle"><gradient android:angle="90" android:centerX="0.5" android:centerY="0.375" android:centerColor="@color/Transparent" android:startColor="@color/Red" android:endColor="@color/Transparent"/></shape></item>
     <item><shape android:shape="rectangle"><gradient android:angle="90" android:centerX="0.5" android:centerY="0.625" android:centerColor="@color/Transparent" android:startColor="@color/Transparent" android:endColor="@color/Red"/></shape></item>
 </layer-list>

I need to programmatically edit the startColor, centerColor, and endColor of each of the GradientDrawable(s) in the LayerDrawable. I am able to access the GradientDrawable(s), but I cannot seem to figure out how to set the mentioned properties. Everything I have been able to find says to either use the SetColors method or create a new GradientDrawable. I have been unable to figure out how to do either of these. The values I will be using for the colors are color resources. How can I edit the GradientDrawable(s)?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

The values I will be using for the colors are color resources. How can I edit the GradientDrawable(s)

Try to get the LayerDrawable object from the view's background in code, then you could get the GradientDrawable via the LayerDrawable.GetDrawable() command. In GradientDrawable class, it provides SetColors method to set the colors.

Here is the related code, you could refer to it.

var layout = FindViewById<LinearLayout>(Resource.Id.layout);
var drawable = layout.Background as LayerDrawable;

var count = drawable.NumberOfLayers;//you could traverse the drawables to reset the colors

//change the colors of the first drawable
var item1 = drawable.GetDrawable(0) as GradientDrawable;
item1.SetColors(new int[] { Color.Blue, Color.Green, Color.Blue });


Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So the int[] parameter contains the ids of the color resources? I figured it was ints representing colors, not resources (although ids is definitely nicer, at least in my case). The other thing I was not 100% sure about was what order the colors should go in. My guess would be
[0] startColor
[1] centerColor
[2] endColor
But I could not find anything documenting this (although luckily it is something that would be easy to figure out through testing). So it is the resource ids that go in the array?

0 Votes 0 ·

My guess would be [0] startColor [1] centerColor [2] endColor

The parameter of GradientDrawable.SetColors method is a color array that must contain at least 2 colors. It also could be added more than three colors. But if you set just three colors, it will work as you guessed.

So the int[] parameter contains the ids of the color resources?

You could set the resource color. And it also supports to use the colors from Android.Graphics.Color.* directly.

item.SetColors(new int[] { Android.Graphics.Color.Transparent, Android.Graphics.Color.Transparent, Resource.Color.colorAccent });
0 Votes 0 ·