3

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?

Snailer
  • 3,777
  • 3
  • 35
  • 46
  • check this tutorial - http://developer.android.com/intl/zh-CN/guide/topics/data/data-storage.html#filesInternal – Vit Khudenko Nov 19 '11 at 22:30
  • The question was not about writing to internal storage.. – Snailer Nov 19 '11 at 23:20
  • The tutorial has a reference to `openFileInput()`. Sorry - I should have pointed to that more explicitly. This way you can "access that file". – Vit Khudenko Nov 19 '11 at 23:28
  • Well I guess you have a point.. maybe I wasn't specific enough. I know how to "access" it, but I need to inflate it. – Snailer Nov 20 '11 at 00:18
  • Sorry for my poor English. I think we can do by creating views in dynamically using tags of the XML. But I do not know it good for android system. – pandu Jul 14 '19 at 03:20
  • Sorry for my poor English. I think we can do by creating views in dynamically using tags of the XML. But I do not know it good for android system. – pandu Jul 14 '19 at 03:20

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Well, it _is_ a standard Android XML file. When the user pushes the save button I'll parse their input into an XML file. I'll look at those classes and post back.. thanks! – Snailer Nov 19 '11 at 23:12
  • Ok, I've got it working (I was playing Skyrim for a bit :P ). I ditched storing the file as a typical Android XML layout and I'm now loading it with `XmlPullParser`. I guess "No, you can't" suffices as an answer ;) – Snailer Nov 20 '11 at 03:25
  • @Snailer: Yeah, it'd be nice to inflate layout files from arbitrary sources. With some work, you might even be able to hack in a solution for that. But it's definitely not supported out of the box. Also, my apologies for assuming that your original file wasn't actually a layout -- it felt more like just ordinary XML-encoded data from what you wrote. – CommonsWare Nov 20 '11 at 12:53
1

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.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • I am also wondering for the same thing. I want to provide multiple themes for my android application. Main app can use layouts loaded in plug in app. So you got any solution ? Or any suggestion, demo, prototype if you found or still looking for the solution ? – Smeet Jul 19 '16 at 06:51