0

Usually, I get the image and binding them to the ImageView something like:

Integer[] icon = {R.drawable.avatar1, R.drawable.avatar2}; 
imageView_1.setImageResource(icon [0]);
imageView_2.setImageResource(icon [1]);

Anybody know is there is any other way to declare the image name on the resource file strings.xml? And how do we add them to ImageView?

makes
  • 6,438
  • 3
  • 40
  • 58
AdrDev_CTS
  • 245
  • 4
  • 11
  • Very vague question. Please clarify what you want to do. – aviraldg Nov 16 '11 at 13:16
  • means that you want to refer avatar images based on the counter varialble like 1, 2,3,4 and so on, is it the case you want to implement? – Paresh Mayani Nov 16 '11 at 13:17
  • If this is the case then here is more info for the same problem: [make variable name from other variable value](http://stackoverflow.com/questions/8135791/make-variable-name-from-other-variable-value/8135837#8135837) – Paresh Mayani Nov 16 '11 at 13:18
  • This line int resID = getResources().getIdentifier(imageID, "drawable", getPackageName()); helped me alot, tks Paresh Mayani .... – AdrDev_CTS Nov 16 '11 at 13:30

2 Answers2

3

To get the resource ID from a resource, such as a translation string or a resource drawable, in general, you can use (as AdrDev_CTS pointed out):

int resID = getResources().getIdentifier(imageID, "drawable", getPackageName());

You can then use this resId in a view you have.

pjv
  • 10,658
  • 6
  • 43
  • 60
0

Note that getIdentifier(imageID, "drawable", getPackageName()) is considered really slow and it's not adviced to use it..

use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Ewoks
  • 12,285
  • 8
  • 58
  • 67