0

I am trying to search for drawable (pictures) in a resource file in android from my library project Is it possible without the context of the of the actual android project or do I need to add that in? Also how would I be able to find an ID through my library project by using a string as the name of the ID and then convert it to the appropriate integer to set the background resource.

This is what I have tried so far: This works but only looks at the rescources in the library project, I need to look at the resources in the current application project

  try
            {
                Class res = R.drawable.class;
                Field field =
                    res.getField("string_ID_I_want");
                drawableId = field.getInt(null);
            }

2 Answers2

0

The actual drawable resources are compiled into your app, so accessing them is no different than accessing normal app resources.

Supposing the ID of the drawable in the library is big_picture. Then R.drawable.big_picture is the ID of the drawable you want to load. You don't have to use introspection. If R.drawable.big_picture is not available, there's something wrong with your project setup (or a compile error).

Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34
-1
Context context = getApplicationContext();
Resources r = context.getResources();
r.getDrawable(id);  //pass your drawable id in it, could be R.id.whatever
StoneBird
  • 1,900
  • 13
  • 12
  • What is I do not know the name of the resource at compile time, but I know it will align with an inputted string of a text file that each user create individually. Essentially can I convert from a string to look for an ID and still get the drawable? – Alexander Joseph Norton Apr 25 '13 at 14:22
  • If you don't know id at compile time then at runtime you need to read file rather than find a drawable as a resource, since drawable does not have `setID()` and `getID` method and you won't be able to reference them by id. – StoneBird Apr 25 '13 at 16:07
  • Or maybe you want to try this link. http://developer.android.com/reference/android/view/View.html#getDrawableState() – StoneBird Apr 25 '13 at 16:16