Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlResourceParser implementation

I need to dynamically load a xml layout from the server. LayoutInflater has inflate methods that use a XmlPullParser. I've tried that, but it doesn't work.

Looking into the Android source code, it turns out those inflate methods are called with a XmlResourceParser. The implementation Android uses is XmlBlock.Parser, but that is not a public API.

Is there a XmlResourceParser public implementation I can use?

like image 547
Mugur Avatar asked Sep 05 '13 16:09

Mugur


2 Answers

You can use a traditional XmlPullParser like described in Android documentation :

InputStream yourRemoteLayout = ...;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(yourRemoteLayout, "someEncoding");
AttributeSet attributes = Xml.asAttributeSet(parser);

Please see what's explained in XmlPullParser documentation for more details.


Edit : From LayoutInflater#inflate() documentation :

Important   For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

What I guess, is that maybe you should make your own implementation of LayoutInflater.Factory2 if Android's own only rely on preprocessed resources.

like image 92
Antoine Marques Avatar answered Sep 20 '22 03:09

Antoine Marques


in fact, you CAN NOT load xml layout dynamically. android system DOES NOT need a XmlResourceParser. when android ui system inflate an resource, it just convert the parser to it's private implementation, a binary xml source parser (i forgot the class name).

1 year ago, i tried this, spent many many times. so, don't waste your time as me again.

like image 23
afpro Avatar answered Sep 24 '22 03:09

afpro