androidandroid-layoutparsinglayout-inflaterandroid-xmlpullparser

Parse layout not from resources and set it in Runtime


I got a thing to think about, but cannot find the solution. New layout file is being received from the server. I faced problem that Parser cannot resolve all those android attributes to inflate view in the next step. We can change file format on server-side to make it as attributes, but how to parse it and set properly still a question. Does anybody have any idea or suggestions how to do that? Thanks in advance.


Solution

  • You can't. Two problems.

    1)You can't parse a general xml file and make a layout from it. From the docs on LayoutInflater.inflate() "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."

    2)You can't save to assets. Assets are read only. Of course you could use the filesystem instead.

    You can get what you want, but you'd have to write your own parser for your layout files from scratch. Its highly not recommended.