27

How can I make an array that handles some of my images so that I can use it like this?:

ImageView.setImageResource(image[1]);

I hope I explained well...

King of Masses
  • 18,405
  • 4
  • 60
  • 77
JoeyCK
  • 1,384
  • 4
  • 19
  • 29
  • 1
    take a look at this [Android storing R.drawable. ids in XML array][1], its using typed array in array.xml. [1]: http://stackoverflow.com/questions/6945678/android-storing-r-drawable-ids-in-xml-array – Hayi Nukman Apr 06 '13 at 13:12

5 Answers5

81

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));
janos
  • 120,954
  • 29
  • 226
  • 236
Walter Mundt
  • 24,753
  • 5
  • 53
  • 61
  • 2
    How would you achieve adding images to an int[] but have it search through all files and add instead of explicitly naming each file to add? – Pj Rigor Sep 29 '16 at 02:41
12

First of all you have to get all drawable IDs of your pictures with the following method:

int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)

For example, you could read all drawable IDs in a loop:

int drawableId = resources.getIdentifier("picture01", "drawable", "pkg.of.your.java.files");
                                          picture02...
                                          picture03...

..and then save them into an Array of Bitmaps:

private Bitmap[] images;
images[i] = BitmapFactory.decodeResource(resources, drawableId);

Now you are able to use your images as array.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Bevor
  • 8,396
  • 15
  • 77
  • 141
3

with respect of @Bevor, you can use getResources() method instead of android.content.res.Resources like this:

ArrayList<Integer> imgArr = new ArrayList<Integer>();

    for(int i=0;i<5;i++){

        imgArr.add(getResources().getIdentifier("picture"+i, "drawable", "pkg.of.your.java.files"));
    }

Usage:

imageView.setImageResource((int)imgArr.get(3));
Hamid
  • 1,493
  • 2
  • 18
  • 32
1
String[] arrDrawerItems = getResources().getStringArray(R.array.arrDrawerItems); // Your string title array

// You use below array to create your custom model like
TypedArray arrDrawerIcons = getResources().obtainTypedArray(R.array.arrDrawerIcons);

for(int i = 0; i < arrDrawerItems.length; i++) {
    drawerItemDataList.add(new DrawerItemModel(arrDrawerItems[i], arrDrawerIcons.getResourceId(i, -1), i == 0 ? true : false));
}
drawerItemAdapter = new DrawerItemAdapter(this, drawerItemDataList);
mDrawerList.setAdapter(drawerItemAdapter);
Pankaj Talaviya
  • 3,328
  • 28
  • 31
1

Kotlin code: initialize below class:

private var drawables: Array<Drawable>

initialize drawables in 1 line:

drawables = arrayOf(
            model.getDrawable(R.drawable.welcome_1),
            model.getDrawable(R.drawable.welcome_2),
            model.getDrawable(R.drawable.welcome_3)
        )

my model.getDrawable() is

fun getDrawable(id: Int): Drawable {
    return ResourcesCompat.getDrawable(context.resources, id, context.theme)!!
}

set drawable to imageView

holder.imageView.setImageDrawable(drawables[position])