7

How can I load vector drawables from an online location e.g http://arsenal.com/image.xml and display it as an image

Lewis McGeary
  • 7,692
  • 2
  • 40
  • 46
Idee
  • 1,887
  • 21
  • 31

1 Answers1

4

Do you need to support VectorDrawables on older devices? If so, then I don't think it is currently possible. AFAIK the support library will only let you assign a VectorDrawable if it is a resource (ie. via setImageResource()).

http://android-developers.blogspot.co.nz/2016/02/android-support-library-232.html

The alternative would be to use SVGs instead and use one of the SVG libraries for Android.

However, if you only need to support Lollipop and later, then it should be possible using the process as set out below. Though I haven't tried it myself.

First, fetch the VectorDrawable file as a Stream. As an example, see this question.

You will then need to make an XmlPullParser instance.

xppXmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();

Then tell the parser about the stream

xpp.setInput(input, null);

Then you can get a VectorDrawable by calling inflate(). Pass it the parser instance.

VectorDrawable  vd = new VectorDrawable();
vd.inflate(getResources(), xpp, null, null);

Then you should be able to assign the drawable to your ImageView.

imageView.setImageDrawable(vd);

Good luck!

Community
  • 1
  • 1
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • This seems not to work: first of all you need to pass a AttributeSet (where can we get this from?) and second, if you have an AttributeSet loading of the XML will fail with a Parser error. It seems like XMLDrawable.inflate can also only work with precompiled (binary) XMLs. – Benjamin Mesing Jan 27 '23 at 14:32