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.

  • Navigation Drawer - what does syncState() do and why it should be called inside onPostCreate()?
  • RecycleView set wrong height for items
  • Java method for android:layout_gravity
  • Facebook SDK 4.7.0 logs ClassNotFoundException for AppEventsLogger
  • React Native - initialProperties Android
  • Android Camera will not work. startPreview fails
  • Related posts:

    Sending a File using Bluetooth OBEX Object Push Profile (OPP)
    sending push notifications to multiple android devices using GCM
    Overriding View.onSaveInstanceState() and View.onRestoreInstanceState() using View.BaseSavedState?
    Android - defining colors by referencing other defined colors
    Android framework debugging in Eclipse
    SL4A vs Ruboto on Android Application Development
  • Calling android native APIs from javascript functions of embedded WebView
  • java.util.ConcurrentModificationException when doing Parse.initialize(this
  • How do I build an Android project using Ant? (IntelliJ IDE)
  • Android widget not showing up on the list after developed
  • Avoid hardcoding the debug mode; Warning in Android Manifest
  • TextView and Button in each row and onListItemClick()
  • 3 Solutions collect form web for “Building/using runtime generated layout XML in Android”

    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.

    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.

    The LayoutInflater.inflate method will take an XmlPullParser as an argument so all you need to do is load your dynamically created XML into a PullParser and pass that into the inflater.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.