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 with Kotlin Improving Our Code Adding an App Icon

How to display from an array of images?

Hi Ben, I want to do an Android version of a simple iOS app I've recently launched. It has quote images, audio files and video links etc (link: https://apple.co/2nTJ3tP).It's loosely based on the iOS version this course. How do I do that? How do I store an array of images and display them? I've taken a coupe of Kotlin courses elsewhere, not sure if they cover this. Also searched on YT, not much luck. Any help would be appreciated. Thanks!

Oziel Perez
Oziel Perez
61,321 Points

Hey your link seems to be broken. It just redirects to the main apple.com page. What exactly are you trying to do? Are you trying to display all images that are stored in an array? Or just one at a time? If the former, you should think about using a List and Adapter so that it doesn't take up too much memory on phones. Also, I'm no expert in programming for images but I would suggest you compress the image before displaying, as another way of saving memory and speeding up your app. What does the app do?

3 Answers

Hi Oziel, thanks for your response, that link takes you to download page which also has screenshots. My iOS app has 4 screens, one with 108 quote images which can be swiped through, another screen for map, one for video links and one for audio mp3 files. I'm focusing on getting the images to display, possibly with a swipe function or at least a random button to generate an image. Someone at StackO helped me with a code snippet: ```var cards= arrayOf(R.drawable.card1,R.drawable.card2,R.drawable.card3,R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7);

button.setOnClickListener {
    var  r = new Random()
    var  n=r.nextInt(7)
    imageview.setImageResource(cards[n]);

}```

It works without the new in new RandomIO. Since I have 108images, I wanted to know how to create a class or a kotlin file where I can create an array and call it in the main file. Hope I'm making sense? Thanks again, appreciate your help.

Oziel Perez
Oziel Perez
61,321 Points

I'll be honest, I haven't built any real world apps, just the ones here at Treehouse, so I'm not very familiar with swipe gestures, but at the very least I can guide you to some documentation to figure this out.

Apparently, there are ways to capture common hand gestures and setting them as event handlers. You can set the event handler to your ImageView like so:

ImageView img = (ImageView) findViewById(R.id.my_view);
img.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // respond to touch events
        return true;
    }
});

I don't know Kotlin, but hopefully this Java code can give you an idea of how it is implemented.

Thanks a lot Oziel, appreciate you taking the time. Cheers.