Theoretical question: load external XML-Layout file in Android
Asked Answered
A

5

12

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.

Andrel answered 4/7, 2011 at 20:52 Comment(1)
did u find any solution to thisPhraseologist
V
5

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.

Verbalism answered 4/7, 2011 at 22:16 Comment(6)
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?Andrel
Very slow in general. Go for json or even binary if you need speed.Verbalism
It would be a bit hard to create JSON from PHP->XML->XSL :(Andrel
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?Andrel
Strange, just found this article: 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"Andrel
developer.android.com/reference/android/view/…, android.view.ViewGroup)Vowel
H
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.

Hortensiahorter answered 4/7, 2011 at 21:44 Comment(2)
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 :)Andrel
If anyone is interested in trying this: there is an open source app AnDroidDraw that has a simple class for starting with. reference: code.google.com/p/droiddraw/source/browse/trunk/AnDroidDraw/src/… - 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.Homozygote
B
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.

Bronze answered 29/10, 2014 at 11:13 Comment(0)
P
1

it's possible, but I found way to load only simple layout:

Resources resources=context.getPackageManager().getResourcesForApplication(targetPackage);
int resID = resources.getIdentifier("widget_layout" , "layout", TARGET_PACKAGE);
XmlResourceParser parser = resources.getLayout(resID);
View themeLayout = LayoutInflater.from(this).inflate(parser, null);
Punish answered 5/8, 2021 at 17:35 Comment(0)
D
-1

Maybe the inflate function of the LayoutInflator works for you.

EDIT: doesn't work yet it seems.

Djokjakarta answered 4/7, 2011 at 22:3 Comment(1)
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.Andrel

© 2022 - 2024 — McMap. All rights reserved.