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
Akshay Shivpuri
2,917 PointsUse multiple background images in Crystal Ball app
Helllo everyone,
I want to carry out a modification in CrystallBall app.
When the user shakes the phone, then every time a new background image should come rather than carrying out the animation in the background crystal image.
Actually the modification will work in such a way that each background image will be associated with a given text of string which we are storing in the array. Thus for "n" strings there will be "n" different images. Thus when a user shakes the phone then , the random string along with it's corresponding background image should appear
Looking forward to the reply Ben Jakuben
Thanks in advance.
2 Answers
Ben Jakuben
Treehouse TeacherThere are different ways you could tackle this, of course. At first, I would recommend using two separate arrays: one to hold the ID of the images you want to use, and one to hold the strings you want to use. You just need to match the correct image and String with the same index in the array.
There is the problem with this approach: it's fragile in that if anything gets changed in the array the whole order could get thrown off. But it's simple and straightforward and worth trying as an exercise.
After that, you would want to tie the image and String together somehow. One option is to use a HashMap like we do in the Blog Reader course. Another is to create a new simple custom object that holds an image ID and a String message.
Also, here's a good discussion on StackOverflow about what method you could use to set the background image dynamically of the ImageView.
Best of luck!
Akshay Shivpuri
2,917 PointsThanks Ben for the quick reply.
I'll go through the Blog Reader app first and learn about HashMap from there. I'll then update you over my progress here.
Cheers, Akshay