tags:

views:

220

answers:

2

Dear all

how can I do this:

I'm trying to find a way to open resources which name is determined at runtime only.

Let me explain in more details

I want to have a XML that references a bunch of other XML files in the application apk.

for the purpose of explaining lets say the main XML is main.xml and the other XML are file1.xml file2.xml...fileX.xml...

what i want is to read main.xml, extract the name of the xml I want (fileX.xml) for example. and then read fileX.XML.

the problem I face is that what I extract form main.xml is a string and I can't find a way to change that to R.raw.nameOfTheFile

Anybody has an idea?

ps: I don't want to: - regroup everything in one huge XML file - hardcode main.xml in a huge switch case that links a number/string to the resource id

Best Regards

Jason Rogers

+2  A: 

I haven't used it with raw files or xml layout files, but for drawables I use this:

getResources().getIdentifier("fileX", "drawable","com.yourapppackage.www");

to get the identifier (R.id) of the resource. You would need to replace drawable with something else, maybe raw or layout (untested).

Mathias Lin
A: 

Hi Mathias Thanks for the answer that work great For those in the same case here is a good explanation on the subject (once I had the name of the function it was simple)

getResources().getIdentifier("fileX", "raw", application package);

also for information the application package can be found in the android manifest.

Thanks a lot

Jason