11

I thinks it's probably not possible for security reason but just want to be sure: is it possible to create layout in Android from an external XML file?

To be exactly: I'm creating XML output with XSL on a remote server. I could create the necessary layout direct on the server and then download it to my Android App.

Radek Suski
  • 1,240
  • 1
  • 13
  • 22

4 Answers4

6

It is impossible. XML layouts in Android are NOT stored as XML. For performance reasons, they are pre-processed during compilation and stored in binary form, and layout inflater only understands that binary form rather than xml.

Jarek Potiuk
  • 12,572
  • 2
  • 50
  • 49
  • Right. I didn't even thought about the compilation. In this case I just wondering how performant is it to parse XML response from remote server with DOM or SAX parser? – Radek Suski Jul 05 '11 at 07:23
  • Very slow in general. Go for json or even binary if you need speed. – Jarek Potiuk Jul 05 '11 at 08:06
  • It would be a bit hard to create JSON from PHP->XML->XSL :( – Radek Suski Jul 05 '11 at 08:40
  • You can create (manually) a kind of JSON string. But I think it's a bit silly this way. But I don't think it will be possible to create a binary file in XSL. Or am I wrong? – Radek Suski Jul 05 '11 at 10:59
  • Strange, just found this article: http://www.ibm.com/developerworks/web/library/x-andbene1/index.html Quote: "Compared to the JSON approach, the XML approach is somewhat faster and less memory-constrained—at the expense of additional complexity" – Radek Suski Jul 05 '11 at 17:54
  • http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup) – someUser Jul 28 '15 at 15:03
3

Of course you can create Views dynamic at runtime, while I'm not shure, that this is the best solution. If you have a look at the internals of Android, every View which is created through XML is called with a Constructor with two parameters: Context and - even more interesting for you - an AttributeSet. I think you have a lot of work with parsing it, while keeping track of the right format. You could at least set the values and build your views yourself in Java depending on Server output.

Rafael T
  • 14,504
  • 14
  • 72
  • 137
  • I suppose it is lot of work. However I realized yesterday that it would not be the best solution to create the layout file on the server as it won't allow me eventually to use this format on other devices (iPhone, Windows Phone). So I think I will have to indeed parse it on the device :) – Radek Suski Jul 05 '11 at 07:28
  • If anyone is interested in trying this: there is an open source app AnDroidDraw that has a simple class for starting with. reference: https://code.google.com/p/droiddraw/source/browse/trunk/AnDroidDraw/src/org/droiddraw/android/ViewInflater.java - I can post a slightly improved one that parses some colors, ImageView placeholders. I'd like to see if someone wants to expand on this. – RoundSparrow hilltx Jul 12 '14 at 18:47
1

YES, right now is possible with ItsNat Droid, take a look:

https://groups.google.com/forum/#!topic/itsnat/13nl0P12J_s

It is still under heavy development but most important features are already implemented.

jmarranz
  • 6,381
  • 2
  • 18
  • 10
-1

Maybe the inflate function of the LayoutInflator works for you.

EDIT: doesn't work yet it seems.

mibollma
  • 14,347
  • 6
  • 44
  • 65
  • Yes, I was looking around it too but I think (or at least I didn't found) it's not possible to add XML layout from a file which isn't located in the resources directory. – Radek Suski Jul 05 '11 at 07:26