26

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
  • 17,833
  • 4
  • 58
  • 76
JoeyCK
  • 1,374
  • 4
  • 17
  • 29

5 Answers5

79

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
  • 115,756
  • 24
  • 210
  • 226
Walter Mundt
  • 23,947
  • 5
  • 51
  • 60
  • 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,539
  • 7
  • 35
  • 48
Bevor
  • 8,006
  • 12
  • 71
  • 133
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,374
  • 2
  • 17
  • 30
0
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,030
  • 26
  • 30
0

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])