Android Runtime Layout - android

In my andriod app, I get a xml at run time.
How do I inflate the view from the uncompiled xml ?
The xml is choosen by the user from his file system.
Its not compiled.
Hence I do not have any reference like "R.layout.my_layout"

Try LayoutInflator.inflate():
LayoutInflator.from(this).inflate(R.layout.my_layout, parent, true);
where this refers to the containing Activity and parent is the ViewGroup you are inflating into. You can also use this with a null parent (and false) if you just want to get back a view hierarchy and add it to a ViewGroup yourself.

This can't currently be done using standard android APIs. The XML files containing the layout needs to be processed during build time to be able to inflate them at run-time
See the documentation of the LayoutInflater here http://developer.android.com/reference/android/view/LayoutInflater.html
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.)
One somewhat tedious option is to write your own XML parser to parse the XML and construct the layout programatically.

Related

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.

Can I use external XML files in for layouts in Android?

This might be a silly question, but I would like to generate a layout xml file for android on the server, and when the user presses a button, the xml file is loaded into the android app.
Is this possible at all?
Instead of that, you can create any view you want, programmatically, without writing them in an xml file.
I do not think it is possible. The layout inflator documentation is here:
From the documentation the constructor:
public View inflate (XmlPullParser parser, ViewGroup root)
states:
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.
Why not just create the xml for the views and build them into the application? That is how Android Apps are typically done. If you want to support totally dynamically generated screens then it is going to be a lot of work. You could do it by developing your own layout sytax, and creating a view that knows how to dynamically render a view from that syntax. Essentially you would need to reinvent the layout inflator thats built into the OS.

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.

XML parser for dynamic layout (dynamically loaded skins)

I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.
I found some tutorials using SAXparser:
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html
and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?
I found a similar but unsatisfactorily answered question here:
How to create a layout file programmatically
which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)
Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.
Thanks!
I had similar requirements and tried the same approach - it does not work.
Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html
Update:
Since OP needs to load XML layouts created at runtime:
Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.
DexClassLoader can be then used to load classes inside apk.
well, android makes the hard work for you, but no all the the work....
first that all you have to forget about parsing xml layouts... instead you can make skeletons layout, that manages his inner childs position, size, etc... and later inflate that 'skeleton' xml with LayoutInflater and obtain a View instance...
When you have that View instance then you can do what you want with it, applying the users preferences like backgrouds, foregrounds colors, position, sizes, etc...
maybe i dont understand your question but you can get any view inflated from a xml resource at compile-time and later apply other style or set another propertys
It seems it is impossible to load the layout & change the skin dynamically according to the doc :
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.)
http://developer.android.com/reference/android/view/LayoutInflater.html

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