Issue
Here is what I have:
In res/drawable-hdpi I have this image:
ar_contact
In res/values I have this file:
<resources>
<string-array name="articles_images_thumbnail_array">
<item>@drawable/ar_contact</item>
</string-array>
</resources>
I want to get the id of my image from the string-array (I will have more than one image)
Now when I tried that in code, I got only the path to the image as a String:
res/drawable-hdpi-v4/ar_contact.png
This is my tried code:
id = resources.getStringArray(R.array.articles_images_thumbnail_array)[articleIndex];
The above Java code returns the path as String, But I want the id of the ar_contact image so I can use it as such:
imageThumbnail.setImageResource(id);
I have tried to use integer-array but I got only a 0 value for all my images.
Solution
You should instead use a TypedArray
as said in Resources API guides :
<resources>
<array name="articles_images_thumbnail_array">
<item>@drawable/ar_contact</item>
</array>
</resources>
Then retrieve the array like this :
TypedArray array = getResources().obtainTypedArray(R.array.articles_images_thumnail_array);
Drawable myDrawable = array.getDrawable(0); // Returns @drawable/ar_contact
Answered By - Antoine Marques
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.