salman khalid
salman khalid

Reputation: 4934

How to Dynamically change drawable's id in setImageResource() Android

My Question is how to change image id from drawable folder in setImageResource() in android. My drawable folder contains icon0.png to icon9.png and i want to change these images in image view dynamically using this

 ImageView iV3;
iV3 = (ImageView) findViewById(R.id.imageView3);
iV3.setImageResource(R.drawable.icon + speed_Arr[2]);

speed_Arr[2] contains any value from 0 - 9. But this didnt change images. Plz help me out. regards.

Upvotes: 3

Views: 2894

Answers (2)

SAMD
SAMD

Reputation: 431

int res = getResources().getIdentifier("< packageName:drawable/imageName >'", null, null); Use this res in your iV3.

Upvotes: 0

lulumeya
lulumeya

Reputation: 1628

public static int getIdentifier(Context context, String name)
{
    return context.getResources().getIdentifier(name.substring(0, name.lastIndexOf(".")), "drawable", context.getPackageName());
}

Above code will return the resource id from name String.

Upvotes: 2

Related Questions