how to change values to the shape programatically on xamarin android?

ozkaro 66 Reputation points
2021-06-21T01:14:11.487+00:00

I trying to implement shapes on my layout page but i have to place in "drawable" folder separately, and i can't call them implicitly, I have to call it within a "ImageView" as background but I need to change color's values for the shape
how can i do that?

thanks in advance!

Developer technologies .NET Xamarin
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-06-21T07:20:33.193+00:00

    Hello ozkaro,​

    Welcome to our Microsoft Q&A platform!

    To change the background color programatically, try to get the shape drawable and then use SetTint to change the color value.

    Check the code:

       //layout.xaml  
       <TextView  
           android:id="@+id/text"  
           android:background="@drawable/custom_shape"  
           android:text="testing..."  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"/>  
         
       //change the background  
       var text = FindViewById<TextView>(Resource.Id.text);  
       Drawable background = text.Background;  
       background.SetTint(Resource.Color.colorAccent);  
    

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.