GideonKain
GideonKain

Reputation: 744

android XML Getting a Resource ID (integer) from XML

I have an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<locations>
<location index="0" asset="@drawable/test1"></location>
</locations>

When I parse thru the XML and try to retrieve the ResourceID, it doesn't return

int ResourceID = xmlParser.getAttributeIntValue(null, "asset", 0);

All other values in XML are being retrieved successfully...what am I doing wrong?

EDIT: I figured it out, I'm not wanting to just grab an Int

Instead of

int ResourceID = xmlParser.getAttributeIntValue(null, "asset", 0);

it's

int ResourceID = xmlParser.getAttributeResourceValue(null, "asset", 0);

Upvotes: 4

Views: 4461

Answers (2)

slayton
slayton

Reputation: 20319

You don't actually need to use the XML file to get the resource ID of your drawable. At compilation the 'R.java' file is written that contains the mappings between resource id's and actual resources . To get the resource Id simply use:

int test1ResId = R.drawable.test1;

Upvotes: 2

blessanm86
blessanm86

Reputation: 31779

if you are having the string as 'test1'. The proper way of getting the resource id is

getIdentifier (String name, String defType, String defPackage);

Eg.

getResources().getIdentifier("test1","drawable","com.app"); 

Upvotes: 1

Related Questions