Storing R.Drawable Ids in Xml Array

Storing R.drawable IDs in XML array

You use a typed array in arrays.xml file within your /res/values folder that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<integer-array name="random_imgs">
<item>@drawable/car_01</item>
<item>@drawable/balloon_random_02</item>
<item>@drawable/dog_03</item>
</integer-array>

</resources>

Then in your activity, access them like so:

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);

// get resource ID by index, use 0 as default to set null resource
imgs.getResourceId(i, 0)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, 0));

// recycle the array
imgs.recycle();

Could android store drawable ids like an integer-array?

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.

Android: How can I make a Drawable array?

To do that, you don't want an array of Drawable's, just an array of resource identifiers, because 'setImageResource' takes those identifiers. How about this:

int[] myImageList = new int[]{R.drawable.thingOne, R.drawable.thingTwo};
// later...
myImageView.setImageResource(myImageList[i]);

Or for a resizable version:

ArrayList<Integer> myImageList = new ArrayList<>();
myImageList.add(R.drawable.thingOne);
// later...
myImageView.setImageResource(myImageList.get(i));

I'm having trouble adding drawable resource ids from a typed array into a custom ArrayAdapter. They all come in as 0

You can use TypedArray to access the resource values stored in the integer-array in your strings.xml . I have cloned your repository and made following adjustments to solve your problem. I have also made a pull request you can merge it. Here is my modification,

strings.xml

<resources>

...

<integer-array name="songsCoverArt">
<item>@drawable/coverart_indigo_big</item>
<item>@drawable/coverart_indigo_big</item>
<item>@drawable/coverart_kissme_big</item>
<item>@drawable/coverart_halfs_big</item>
<item>@drawable/coverart_create_big</item>
<item>@drawable/coverart_bigger_big</item>
<item>@drawable/coverart_raveon_big</item>
<item>@drawable/coverart_yank_big</item>
<item>@drawable/coverart_lineup_big</item>
<item>@drawable/coverart_hurry_big</item>
<item>@drawable/coverart_hurry_big</item>
<item>@drawable/coverart_proud_big</item>
</integer-array>

...

</resources>

MainActivity.java

...

private void prepareDefaultContent() {
ArrayList<Song> songs = new ArrayList<>();
//Resources r = recyclerView.getContext().getResources();

String[] songTitle = getResources().getStringArray(R.array.songsTitles);
String[] albumTitle = getResources().getStringArray(R.array.albumTitle);
String[] songLength = getResources().getStringArray(R.array.songsLength);
String[] artistName = getResources().getStringArray(R.array.artistName);

TypedArray typedArray = getResources().obtainTypedArray(R.array.songsCoverArt);

for (int i = 0; i < songTitle.length; i++) {

int coverArtResourceId = typedArray.getResourceId(i, 0);

Log.d(TAG, "Adding = " + songTitle[i] + " " + albumTitle[i] + " " + songLength[i] + " " + artistName[i] + " " + coverArtResourceId);

songs.add(new Song(songTitle[i], albumTitle[i], songLength[i], artistName[i], coverArtResourceId));

}

typedArray.recycle(); // Important

songAdapter.addItems(songs);
recyclerView.setAdapter(songAdapter);

}
...

SongDetail.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_detail);

...

int BigCoverArt = intent.getIntExtra("coverArt", 0);

...
}

I hope it helps. Let me know if you face further difficulties?

Drawable array from xml into int for recycler adapter

Thanks for your answer, but I didn't try it. Maybe it works but I found a better, and probably intended solution. Instead of using setImageResource() I'm using setImageDrawable()

Integer array of resource ids returns 0

Do this:

TypedArray ta = getResources().obtainTypedArray(R.array.array_category_icons);
Drawable[] icons = new Drawable[ta.length()];
for (int i = 0; i < ta.length(); i++) {
int id = ta.getResourceId(i, 0);
if (id != 0) {
icons[i] = ContextCompat.getDrawable(this, id);
}
}
ta.recycle();


Related Topics



Leave a reply



Submit