Access & Modify Drawable Resources

Nathan Sokalski 4,111 Reputation points
2020-11-29T18:43:31.1+00:00

I have an XML file in my Resources/drawable folder with the following:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/Transparent"/>
    <stroke android:width="3dp" android:color="@color/White"/>
    <corners android:radius="27dp"/>
</shape>

I want to access (and possible modify) this in my codebehind (specifically a custom renderer). In XAML, it can be accessed using code such as @drawable/circlebuttonborder
But this obviously does not let me modify it. How do I access it in codebehind? Thanks.

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

5 answers

Sort by: Most helpful
  1. Nathan Sokalski 4,111 Reputation points
    2020-11-30T17:24:49.877+00:00

    Where does that access the drawable resource? I am new to Xamarin.Forms and Xamarin.Android, but it looks like your code accesses the already defined Background property of a control, I am looking to access the contents (the shape element) of a *.xml file located in the drawable folder.

    0 comments No comments

  2. Joe Manke 1,091 Reputation points
    2020-11-30T22:47:15.963+00:00

    There are a few options:

    context.GetDrawable(int id) // deprecated
    ContextCompat.GetDrawable(Context context, int id)
    ResourcesCompat.GetDrawable(Resources resources, int id, Theme theme)
    

    For all 3, you need to get a Context from somewhere, in your case of a custom renderer the class will have a Context property.
    You can get the id from the static class Resource (in your case Resource.Drawable.circlebuttonborder).
    For the ResoucesCompat option, you can just pass null for the theme.

    You will want to cast the Drawable returned by any of these to a ShapeDrawable.

    0 comments No comments

  3. Nathan Sokalski 4,111 Reputation points
    2020-12-01T02:25:03.293+00:00

    Thanks! I added the following using statement:

    using Android.Graphics.Drawables.Shapes;

    And I am attempting to get the shape using the following:

    ShapeDrawable circlebuttonborder = (ShapeDrawable)this._context.GetDrawable(Resource.Drawable.CircleButtonBorder);

    Hopefully this is correct (if not, please tell me), but now I am not quite sure how to access the different parts of the shape element inside the xml file. ShapeDrawable does not seem to have these properties, and since the xml element is shape (rather than ShapeDrawable), I'm not quite sure where to look. My ultimate goal is to, after modifying the drawable (or ShapeDrawable or whatever the appropriate class is), to assign it to the Background property of an AppCompatButton. What should I do to modify the ShapeDrawable? Thanks!


  4. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2020-12-04T07:49:35.723+00:00

    Hello,

    Welcome to Microsoft Q&A!

    We need to set the shape as background on the control first , and then get its background to modify the property .

    Sample code

       GradientDrawable bgShape = (GradientDrawable)btn.Background;  
       bgShape.SetColor(Color.Pink);  
       bgShape.SetCornerRadius(0);  
       bgShape.SetStroke(5,Color.Black);  
    

    45122-111221.gif

    Thank you.


    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 comments No comments

  5. Nathan Sokalski 4,111 Reputation points
    2020-12-04T17:31:55.353+00:00

    I have tried that, unfortunately, SetStroke does not seem to be available.


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.