AdrDev_CTS
AdrDev_CTS

Reputation: 245

Android: How to get image from resource file

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?

Upvotes: 0

Views: 1473

Answers (2)

Ewoks
Ewoks

Reputation: 12435

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.

Upvotes: 0

pjv
pjv

Reputation: 10708

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.

Upvotes: 3

Related Questions