1

I have looked everywhere but cannot find an example of programmatically setting a background resource from a string value?

As an example:

Drawable a = getResources().getDrawable( R.drawable.a );
Drawable b = getResources().getDrawable( R.drawable.b );
Drawable c = getResources().getDrawable( R.drawable.c );
abc.setBackgroundResource("b");

Is this possible or would I have to do it as a big switch statement?

Apqu
  • 4,630
  • 7
  • 40
  • 64
  • possible duplicate of [Dynamic Resource Loading Android](http://stackoverflow.com/questions/3648942/dynamic-resource-loading-android) – Simon Mar 17 '14 at 08:34

1 Answers1

4

you have getResources().getIdentifier for this purpose. It returns the id of the resources from its name.

E.g.:

 int resId = getResources().getIdentifier("b", "drawable", getPackageName());

Here you can find the documentation.

Blackbelt
  • 152,872
  • 27
  • 286
  • 297
  • 1
    Oh wow, you learn something new everyday, had no idea! Thanks for your answer, I will accept as soon as I am able to :-) – Apqu Mar 17 '14 at 08:36