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
Peter Hansen
2,255 PointsArray of images in separate file
Hi. I am working on an Android app. When the user clicks a button the ImageView should dynamically switch the images in random order. It is all working in one file, but how do I get the arrays of images in a separate file when using the 'ContextCompat.getDrawable(this, R.file). Thanks in advance, Peter.
MainActivity.java private Drawable drawable; private Drawable[] drawables = null; private Random random;
// Store the location of images inside the array drawables = new Drawable[]{ ContextCompat.getDrawable(this, R.mipmap.dog_1), ContextCompat.getDrawable(this, R.mipmap.dog_2) };
// OnClickListener på button quoteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Switch images random = new Random(); int randomNumber = random.nextInt(drawables.length); drawable = drawables[randomNumber]; dogImageView.setImageDrawable(drawable); });