Programmatically Edit Colors in the Gradient of a GradientDrawable

Nathan Sokalski 4,126 Reputation points
2021-08-08T21:20:55.957+00:00

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)?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-08-09T06:02:08.327+00:00

    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.


0 additional answers

Sort by: Most helpful