1

In android arrays.xml, I can define arrays like below,

<string-array name="feed_icons">

    <item value="0">@drawable/latest</item>

    <item value="1">@drawable/video</item>

    <item value="2">@drawable/world</item>

    <item value="3">@drawable/sports</item>

    <item value="4">@drawable/arts</item>

    <item value="5">@drawable/dining</item>

</string-array>

I wonder how to get the drawable according the value? Is three any function? I just kown that,

String[] iconArrays = getResource().getStringArrays(R.arrays.feed_icons);

but it's not my desired.

2
  • Next time post your question on the right site. Commented Nov 5, 2014 at 11:51
  • you have given value as 0 to n so you can get appropriate drawable from String[] as position wise which indirectly value. Commented Nov 5, 2014 at 11:58

1 Answer 1

5

You could save the drawable names in an array like:

<string-array name="feed_icons">
    <item value="0">latest</item>
    <item value="1">video</item>
</string-array>

Then fetch the drawable:

String[] iconArrays = getResources().getStringArray(R.arrays.feed_icons);

// Get specific drawable resource id
int drawableResId = this.getResources().getIdentifier(iconArrays[0], "drawable", getPackageName());

// Fetch drawable by resource id
Drawable yourDrawable = getResources().getDrawable(drawableResId);

// Use drawable
imageView.setDrawable(yourDrawable);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.