65

I've tried this:

r = Resources.getSystem().getIdentifier("ball_red","drawable","com.Juggle2");
Log.i("FindBall","R = "+r);

And this:

r = Resources.getSystem().getIdentifier("com.Juggle2:drawable/ball_red", null, null);

But 'r' always ends up as zero.

I'm calling this line from inside a helper class that's not an Activity and doesn't extend anything, so I can't simply call getResources(), but I can pass it from my SurfaceView.

Eventually, I want to replace "ball_red" with a variable, but first thing's first. This isn't working.

com.Juggle2 is indeed my package name. drawable is the res folder that it's in, and, the name of the file is indeed ball_red.

R.java says:

        public static final int ball_red=0x7f020027;

So I'm not sure why it isn't working.


So I can't use Resources, I must pass a context, and I'm doing that this way: Inside here:

class Collection extends SurfaceView implements SurfaceHolder.Callback {

I'm making a new instance of my class and passing it getContext() as a parameter.

  • 2
    As per documentation of `Resources.getSystem()`: *"Return a global shared Resources object that **provides access to only system resources (no application resources)**, and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc)."* As answered by @Sajmon, you'll need to pass in a `Context` instance to your (static) helper method to access application-specific resources. – MH. Mar 18 '13 at 23:56
  • btw the type aspect is case sensitive, e.g. "strings", "drawable" and not "Strings", or "Drawable" – Ed Manners Apr 03 '17 at 14:41

3 Answers3

146

Since you are inside of an activity it is enough to write

int resId = YourActivity.this.getResources().getIdentifier(
    "ball_red",
    "drawable",
    YourActivity.this.getPackageName()
);

or if you're not calling it from an inner class

int resourceID = getResources().getIdentifier(
    "ball_red",
    "drawable",
    getPackageName()
);

Note

getIdentifier() Returns 0 if no such resource was found. (0 is not a valid resource ID.)

Check

Check also in your R.java whether there is a drawable with the name ball_red

e.g.:

public static final class drawable {
        public static final int ball_red = 0x7f020000;
 }

EDIT If you're not in any activity then you must pass a context instead of resources as parameter then do this

int resId = context.getResources().getIdentifier(
    "ball_red",
    "drawable",
    context.getPackageName()
);
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • I'm not calling it from an activity, and I don't have access to `getResources()`, but I can pass Resources in from a `SurfaceView` class, so I'll try that –  Mar 18 '13 at 22:45
  • Yep, the `drawable` is there. –  Mar 18 '13 at 22:50
  • @OWiz If you're not in any activity then you must passe a context instead of resources as parameter then do this int resId=context.getResources().getIdentifier("ball_red", "drawable", context.getPackageName()); – Festus Tamakloe Mar 18 '13 at 22:59
  • Tried that. Context came up null. Investigating –  Mar 18 '13 at 23:04
  • @OWiz try then to passe getApplicationContext(). If there is styl null then there is problem with your app. sO far the context is null, you can not access resources too – Festus Tamakloe Mar 18 '13 at 23:09
  • Okay, so I figured out why it was null, and stopped that, so now it works. –  Mar 19 '13 at 00:28
6

For Xamarin users I had the issue where I had added an icon with lower and uppercase letters (e.g. iconVaccine.png ) and was referring to the uppercase name iconVaccine.

Xamarin will allow you to do this (even though you shouldn't), but when the app gets compiled the name are flattened to lower case, so you must refer to the lower case variant as follows:

Image Name: iconVaccine.png

Xamarin reference: iconVaccine (as created in Resource.designer.cs, but will fail)

Correct Reference: iconvaccine

Hope that helps!

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Craig Champion
  • 442
  • 9
  • 16
4

Though the answer from Festus Tamakloe is correct I found a quirk in this function.

If you are declaring a string-array in a xml file it must be accessed by calling the base resource type array, using string-array results in anid 0 return.

Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59