1

I'm trying to get a resource from XML using an array of strings because it's currently in a loop.

Can anyone advise me on how to do this?

The resource name is the same name as the array string name just to be clear.

So far I have tried:

mMainEngine.mContext.getString(R.string.class.getField(Modification.ModicationNames[Current]).getInt(null)),
3
  • you have the name of a resource and you want to get its id? Commented May 24, 2013 at 10:35
  • The resource name is the same as the array string name Commented May 24, 2013 at 10:36
  • you need this Commented May 24, 2013 at 10:36

2 Answers 2

2

I have not missunderstood you

int id = getResources().getIdentifier("name_of_resource","string", getPackageName());

is what you neeed

Sign up to request clarification or add additional context in comments.

4 Comments

The method getPackageName() is undefined for the type ModificationContainer
you need a context. is ModificationContainer an activity?
The error is "android.content.res.Resources$NotFoundException: String resource ID #0x0" but the string defiantly exists.
put string instead of id. See my edit. getIdentifier("name_of_resource","string", getPackageName());
0

Just for example I am accessing Drawable. You can get id from name as follows

Resources res = context.getResources();
String strpckg = context.getPackageName();

int id = res.getIdentifier(iconName, "drawable", strpckg);

further you can access Drawable as :

Drawable drawable = res.getDrawable(id);

1 Comment

This is for drawable you can do same for String or any resource that you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.