XML parser for dynamic layout (dynamically loaded skins)

I want to write an application where (at least for now) the content is always the same, but the layout is loaded dynamically at runtime based on user preferences. In fact, I want the application to use a β€œskin”, which may look completely different than other skins.

I found several 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 something from scratch that recognizes all the standard XML layout tags and then dynamically loads each part of the layout. But it is a lot of work to do from scratch! Of course, this function is available in android, or, for sure, someone wrote some open source code that can be run at the beginning of your action using the WithCreate method, which accepts an XML file and sets your layout?

I found a similar but unsatisfactory answer to the question: How to create a layout file programmatically, which makes me think that since setContentView should take an integer resourceID as an argument, the fact that they were previously baked at compile time can be a problem. (setContentView can also take a View object as its argument, but I don’t want tons of if statements to pass each View object one by one, I want some code to enter an XML file or xml string and set the content to look.)

Maybe I'm going out of the way. Is there any other way to do this? I would think that it is important to have an application with dynamically loaded skins.

Thanks!

+7
source share
3 answers

I had similar requirements and tried the same approach - it does not work.

The documentation clearly states the following: http://developer.android.com/reference/android/view/LayoutInflater.html

Update:

Since the OP needs to load the XML layouts created at runtime:

Perhaps this can be done by creating XML layout files, copying them to a dummy project, create .apk, and then download apk to the device.

DexClassLoader can be used to load classes inside apk.

+2
source

well, android does the hard work for you, but not all work ....

first, all you need to forget about parsing xml layouts ... instead, you can make a skeleton layout that controls its internal child position, size, etc .... and then inflate this "skeleton" xml with LayoutInflater and get view instance ...

When you have a View instance, you can do what you want with it, applying user preferences such as backgrouds, foreground colors, position, size, etc.

Perhaps I do not understand your question, but you can get any view that is inflated from the xml resource at compile time, and then apply a different style or set other properties

0
source

It seems that it is impossible to load the layout and dynamically change the skin according to the document:

Therefore, it is currently not possible to use LayoutInflater with an XmlPullParser on top of a regular XML file at run time; it only works with the XmlPullParser returned from the compiled resource (R.something file.)

http://developer.android.com/reference/android/view/LayoutInflater.html

0
source

All Articles