Creating/Editing VectorDrawable in Code

Nathan Sokalski 4,121 Reputation points
2021-09-23T18:10:42.61+00:00

I have a VectorDrawable that I need to edit during runtime. I know how to create a VectorDrawable Resource using xml, but I need to edit the pathData of the path element during runtime. I am having trouble finding the documentation on & how to edit the different components of a VectorDrawable in codebehind. Any ideas?

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,741 Reputation points Microsoft Vendor
    2021-09-24T04:47:26.727+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you xml file put in the Assets folder, you can use this way to edit the xml at the runtime,

       XmlDocument xmlDocument = new XmlDocument();  
         
                   xmlDocument.Load(Assets.Open("MyVictor.xml"));  
         
                   XmlNodeList tagList = xmlDocument.GetElementsByTagName("path");  
         
                   foreach (XmlElement xmlElement in tagList)  
                   {  
                       XmlAttribute name = xmlElement.GetAttributeNode("android:pathData");  
                       name.Value = @"M15.5,14h-0.79l-0.28,-0.27C15.41,12.59";  
                       //if (name.Equals("pathData"))  
                       //{  
                       //    xmlElement.InnerText = @"M15.5,14h-0.79l-0.28,-0.27C15.41,12.59";  
                       //}  
         
         
                   }  
    

    Best Regards,

    Leon Lu


    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