Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could android store drawable ids like an integer-array?

I want a drawable id array of integer values which I can store like an integer-array in res/values/XXX.xml by using integer-array tag. Below is integer-array declared in strings.xml

<integer-array name="icons">
     <item>1</item>
     <item>2</item>
     <item>3</item>
     <item>4</item>
</integer-array>

But I want to store drawable image ids like @drawable/someImage as an integer array in xml.

OR Is there any alternatives to store drawable integer ids as an integer array in xml.

like image 227
Pankaj Avatar asked Apr 23 '15 09:04

Pankaj


People also ask

What are Drawables in Android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is drawable class in Android?

A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms.

How do I add an image to a drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.


1 Answers

I think TypedArray is what you are looking for. I have samples using it. If you are interested, take a look at codes below:

First, integer-array in res/values/arrays.xml:

<integer-array name="frag_home_ids">
    <item>@drawable/frag_home_credit_return_money</item>
    <item>@drawable/frag_home_transfer</item>
    <item>@drawable/frag_home_balance</item>
    <item>@drawable/frag_home_charge</item>
    <item>@drawable/frag_home_finance_cdd</item>
    <item>@drawable/frag_home_finance_ybjr</item>
    <item>@drawable/frag_home_more</item>
</integer-array>

Second, get resource integer values programmatically:

TypedArray tArray = getResources().obtainTypedArray(
            R.array.frag_home_ids);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
    ids[i] = tArray.getResourceId(i, 0);
}
//Recycles the TypedArray, to be re-used by a later caller. 
//After calling this function you must not ever touch the typed array again.
tArray.recycle();

Third, call the integer values like this:

holder.iv.setImageResource(ids[position]);

Of course, you can get integer values of string, color, integer, layout, menu...in this way.

I hope these codes will inspire you.

like image 194
SilentKnight Avatar answered Sep 22 '22 02:09

SilentKnight