0

Possible Duplicate:
Setting Android images from string value

I need to change background image programmatically on Android with source string I have. For example I have an array of strings gotten from XML file "source1", "source2" etc., so how can I set them to background as images? I saw examples like mTextView.setBackgroundResource(R.drawable.myResouce);, but I don't understand how to set mySource from a String. May be it is possible through drawable object?

Community
  • 1
  • 1
Arthur Kushman
  • 3,449
  • 10
  • 49
  • 64

1 Answers1

5

Keep your images name inside drawables same as of in String.xml.

Say for eg: images.png it should be images in String.xml

If you pass this name to getResourceId() it will returns you Drawable with that String Variable.

public static int getResourceId(Context context, String name, String resourceType) {
    return context.getResources().getIdentifier(name, resourceType, context.getPackageName());
}

int iconId = getResourceId(Activity.this, image, "drawable");    

ImageView categoryIcon = (ImageView) v.findViewById(R.id.category_icon);
categoryIcon.setImageResource(iconId);
Community
  • 1
  • 1
Venky
  • 11,049
  • 5
  • 49
  • 66