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

Crystal Ball Android app help

I'm still pretty new to android development and programming in general.

Really enjoying treehouse. I've done a quick run through of 'Build a Simple Android App' and am now in the process of going through it a second time in more depth.

Can someone help me to achieve the following:

Lets say I wanted a certain image and sound file to play corresponding to a certain random answer every time I shook the phone.

e.g. The answer text is 'It is certain', I want an image/animation I've created that says 'certain' to show and an mp3 file that says 'it is certain' too. There will be a separate image and mp3 for each answer.

Thanks.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi @Faraz,

I love what you're trying to do here! Here are some thoughts that might help. If you get it working, upload it on Google Play and let us know to take a look! :smile: :thumbsup:

So right now the onShake() event calls handleNewAnswer(). And handleNewAnswer() does the rest of the work:

  1. Animate the crystal ball (animateCrystalBall())
  2. Animate the answer text (animateAnswer())
  3. Play the crystal ball sound (playSound())

Let's talk about the sound first, as that's very straightforward. You'll need to load all your sounds in the res/raw directory just like crystal_ball.mp3. Then you'll want to add a String parameter (or whatever) to the playSound() method that passes in the answer String (or some code if you prefer). Then in the playSound() method, create an int variable that you use as the second parameter of the MediaPlayer.create() call. Right now it's hard-coded to R.raw.crystal_ball, but you would just need to change the audio file based on whatever answer String gets passed in.

As for the images, you'll need to add an ImageView to the layout in a manner that matches how you want it to work. Feel free to ask more questions about that in here if you get stuck with it. Then you could maybe add a new method call in handleNewAnswer(), like showImage(String answer). This method could set the image for the new ImageView based on whatever image files you dump in the res/drawable folders.

Hope this helps!

Thanks for the help Ben!

I'll continue with the project when I have some free time.

I'll let you know if I do upload it on Google Play or if I need any further help.