Issue
Here is the situation: I Have two projects. Let's say a LibraryProject
and a MainProject
. The MainProject
references the LibraryProject
as a library.
I have one activity in the LibraryProject
that needs to discover if the MainProject
have defined a specific drawable, let's say "logo.png" (Think that the logo image must be defined by each `MainProject, and not by the LibraryProject.
How to, in one activity of the LibraryProject
, discover if the MainProject
has this image in the res/drawable
folder?
Obviouslly I have tried to see if R.drawable.logo != 0
(or variation of it), but as you know, this line will not compile, since the image is not in the res/drawable
folder of the LibraryProject
.
I also tried getResources().getIdentifier("logo", "drawable", null) != 0
but this boolean expression is always returning false, since the .getIdentifier()
always returns zero.
Any idea?
Solution
You can try this: (but remember, the context is always "ChildProject")
public static Drawable getDrawable(Context context, String resource_name){
try{
int resId = context.getResources().getIdentifier(resource_name, "drawable", context.getPackageName());
if(resId != 0){
return context.getResources().getDrawable(resId);
}
}catch(Exception e){
Log.e(TAG,"getDrawable - resource_name: "+resource_name);
e.printStackTrace();
}
return null;
}
Answered By - Guido
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.