Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML parser for dynamic layout (dynamically loaded skins)

Tags:

android

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!

like image 998
dnh37 Avatar asked Feb 23 '11 15:02

dnh37


1 Answers

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.

like image 105
Peter Knego Avatar answered Oct 21 '22 06:10

Peter Knego