Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building/using runtime generated layout XML in Android

I am currently working on a project which requires me to use an XML document to render a form on an Android device. The form must be fetched and displayed at run-time. I am wondering if there is a way to tag the form XML, transform it using XSLT into an Android layout XML, and then have the device render it.

like image 888
illvm Avatar asked Dec 21 '09 19:12

illvm


People also ask

How do I create an XML layout in Android Studio?

Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.

How to declare UI elements in XML in Android?

Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.

What is a layout in Android?

Your layout is the architecture for the user interface in an Activity. It defines the layout structure and holds all the elements that appear to the user. You can declare your layout in two ways: Declare UI elements in XML.

What is layout parameter in Android?

Layout Parameters Layouts Part of Android Jetpack. A layout defines the structure for a user interface in your app, such as in an activity. All elements in the layout are built using a hierarchy of View and ViewGroup objects.


2 Answers

Unfortunately you can't just clone LayoutInflater or use other such tricks to do this -- layout inflation is entirely dependent on the view constructors which take an AttributeSet argument, which are entirely dependent on the Context.obtainStyledAttributes method, which itself is entirely dependent on having a pre-processed binary XML file to be able to do reasonably efficient attribute resolution.

An alternative approach you can explore is to use the aapt tool (or more likely a hacked version of it) on your server, to compile the layouts you generate into the appropriate data. Unfortunately we don't currently have a way to contruct an XmlPullParser from a raw binary blob (it must get this blob from the AssetManager), so there is a fair amount of work to do on both the client and server with this approach. I suspect one can come up with something pretty neat, but it will be lots of work.

like image 58
hackbod Avatar answered Sep 28 '22 16:09

hackbod


Android only contains a built-in way to "inflate" layout XML stored as a layout resource in the APK file. If you want to "inflate" similar (or different) XML from other sources, you will have to implement that yourself, perhaps by cloning some logic from the LayoutInflater class.

like image 39
CommonsWare Avatar answered Sep 28 '22 16:09

CommonsWare