Setting layout by remote xml - android

I was wondering if it was possible to set a view through a remote xml file. I had a look on the web and I found this post here on stackoverflow. Reading the answers I got I can't do it.
Why?

XML layout files are pre-processed at build time in order to provide efficient inflation of complex layouts.
Although there are LayoutInflator methods which take a path to an XML file, they have never been implemented.
In other words, unless your XML layout file is pre-processed and packaged at build time into your APK, then it can't be done.
There is one possibility, however, you could build an XML parser to parse your 'external' XML layout file and create your layout dynamically using Java code - not impossible but you're pretty much on your own if you choose to do that.

Related

Android - Change activity contentview in runtime

I'm developing a generic Android application that needs to get XML files from the server and set its contentview accordingly.
The XML files can be as simple as a relative layout with a textview or much more complex, that's really not relevant. The goal is to simply fetch XML files externally and display them or receive a string from the server, create a XML file locally on runtime and use it, both will do.
I've been looking for a while now and I didn't find any solution. Is there a way to do this?
According to this post which references Android's LayoutInflater doc; it is not possible since there's some "pre-processing of XML files that is done at build time."
In short, you can't use a regular xml pulled externally to inflate.

Android Dynamic Layout and Code from Web

I am working on a project which will have lots and lots of Layout and Code changes.
My Question is pretty simple but I don't know anything about it. Would it be possible to dynamically load content such as Source Code (Java) and XML Layout Files on a Backgroundthread and then startup another Activity with the downloaded Content (Of either/and Java File + XML Layout)?
So that you change your Code and Layout online and download it on every start of the App?
Thanks alot in advance.
You can load classes dynamically. But as for xml layouts that is hardly possible sobeit you are going to write your own parser and inflater. Unfortunately LayoutInflater can't inflate external files. This is from LayoutInflater documentation:
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; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
So you are basically restricted to create all your UI from the code only.

Inflate XML file from internal memory

I've copied an XML file from /assets to my applications data folder (data/data/package.name/files/). I'm doing this because the user will be able to modify a lot of data, and I want to save that data to the internal memory and then load it again when they restart the app. This all works well, using Root Browser I can see the XML file is properly copied to the data directory.
Now I need to inflate this XML file using a LayoutInflater. How would I access this file? With an XmlResourceParser or XmlPullParser?
You cannot inflate that file using LayoutInflater. First, you can only inflate layout resources, not arbitrary files. Second, based on the description in your first paragraph, it is not a layout file in the first place.
If you want to parse arbitrary XML, use the DOM, SAX, or XmlPullParser.
From the Android view API:
it is not currently possible to use LayoutInflater with an
XmlPullParser over a plain XML file at runtime.
A solution might be to pre-process XML files before. You must compress the XML file in the same way the Android SDK does at build time:
view inflation relies heavily on pre-processing of XML files that is done at build time
I guess you will have to call XmlPullParser.setInput to load your pre-processed XML, and then pass the XmlPullParser to View.inflate. I hope it is possible to do this kind of stuff even for content that is not in the APK.
I am very interested in any solution you can find. Please let me know if you find or create a prototype! This would be a decisive step towards creating a plugin architecture for my Open Source app.

Deserialize Android Layout XML?

Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code? Does a tool like JAXB work and if so how do I use it?
It's an interesting idea, though a bit useless IMHO. Why would one want to do it? Inflating an XML is very fast operation and hides a lot of complexity - for example it takes into account what is the current DisplayMetrics and recalculates layout parameters (width/height) appropriately to the density and size of the screen... It's also very fast because it does not actually require XML parsing - parsing is done at compilation time and what is actually stored by android is a binary version of the layout which is optimized for efficiency (that's why you cannot build layout XML dynamically from an XML file).
If you would like to modify the Java code and add/remove some elements then it is much more efficient to inflate the XML and then do all the modifications -less clutter simpler code and all the calculations are done for you...
Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code?
Step #1: Parse the XML.
Step #2: Generate the Java code. You'll have to pray that you can build your own AttributeSet implementation that works with your generated code, otherwise this will be a very complex problem.
Does a tool like JAXB work
AFAIK, JAXB needs an XML Schema, and there is no such schema possible for the Android layout file.
Whatever problem you're trying to solve this way, there's probably a better solution.
I just want a simple way to take an android xml layout and pass it to, say, a command line tool to generate the equivalent Java.
I seriously doubt that this will be "simple" for any reasonable definition of the term.

How to create a layout file programmatically

I was wondering if it were possible to dynamically create an XML layout file to be displayed to the user. The idea would be to be able to retrieve a layout file from a central server, which could display this dynamic, server driven GUI.
XML layout files are packaged as binary and the inflation occurs from binary as well. I don't believe that arbitrary XML can be used for layout.
You'll have a problem with the way resource IDs are pre-baked at build time.
Since GUI elements can be instantiated at runtime, you could probably roll your own inflater with an XML parser and a bit of reflection.

Categories

Resources