11

I am trying to use an array of images then set my ImageView to one of the images in the array. My first instinct was to use an array of strings with the image names but this did not work.

How do I do this.Make a drawable array with the image names without quotes or what?

3 Answers 3

16

depends where your images are

but if there a in R.drawables. then why not simply store the int reference in your array and then load that

if you want to do treatment on them you can also open the Bitmap and store that

edit:

  private int[] textureArrayWin = {
R.drawable.star_00,
R.drawable.star_01,
R.drawable.star_02,
};

and now you have a table of the id of the images you want

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, They are in the drawable-mdpi folder.Not sure what you mean by storing the int reference in my array and then loading that.Really knew to android.
That was not the question
3

Yes you could create an array of drawables.

As an alternate you could also create an array of ints which map to your resource ids. So drop your images in the drawable folder which gives them resource IDs.

R.drawable.yourimage1
R.drawable.yourimage2
...
R.drawable.yourimagen

Then when you want to load an image and draw to your imageview you do something like this from your Activity. Assuming "yourarray" is the array:

Drawable d = getResources().getDrawable(yourarray[n]);

Then I believe the call is setImageDrawable on ImageView. So:

yourImageView.setImageDrawable(d);

1 Comment

Go with the alternate solution rreeverb describes. You don't have to 'get' the drawable, you can simply set the image resource ID on the view! developer.android.com/reference/android/widget/…
0

You can use the resources from drawable-mdpi and store them in an array of integers like

private static final Integer[] Icons = {
    R.drawable.bg_android_icon,
    R.drawable.bg_sunrise_icon,
    R.drawable.bg_sunset_icon,
    ......
};

And then you can use it in a loop with Postion ranging from 0 to Icons.length and set it to ImageView

ImageView.setBackground=getResources().getDrawable(Icons[Position]);

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.