Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Improving Our Code Adding More Colors

Bradley St John Jones
Bradley St John Jones
2,286 Points

How would I make an Image Array?

I want to also add an image to appear and change as the button is pressed but I'm not sure how to make an Image Array.

I assumed it would be similar to the two String arrays in this course but instead using ImageView:

public ImageView[] = {

R.drawable.image1,
R.drawable.image2,

};

But this doesn't work and I am struggling to figure out what format to put them in so they are recognised. Am I missing something obvious or is it that images cannot be put into an array like this?

Any help would be greatly appreciated

Bradley

1 Answer

william parrish
william parrish
13,774 Points

Resources from the R.xx.xx portion of your app, are given an Integer constant as an identifier. If you wanted to then make an array of R.drawable.image you could just make it an int[]

 int[] mArray = new int[10];
    mArray[0] = R.drawable.image1;
    mArray[1] = R.drawable.image2;
Bradley St John Jones
Bradley St John Jones
2,286 Points

Thank you William, that seems to have worked.