Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top