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
Alfred van Kuik
2,851 PointsCrystalBall app - fade in randomly doesn't work
I followed along with the crystal ball instructions, and the app is working great. Except for one thing... The fade in animation doesn't seem to work all the time. It randomly works perfectly or it doesn't do anything. Sometimes my answers fade in, sometimes they just appear without the animation. There doesn't seem to be a pattern to this. I did a few amateuristic attempts to fix it (including trying an isRunning() check, which apparently isn't possible on an AlphaAnimation :P), without success. It happens only on my phone (Sony Xperia S running android 4.1.2), not in the emulator
So, did anyone else run into this problem? Is there a way to fix it?
1 Answer
Adrian Thomas
1,043 PointsOdd thing with me, is that the fade in doesn't work period. I don't get any text after the animation has ran. Yet when I run the debug, it steps through the application normally. I've tried this on the HTC One, anyone experience similar?
Ben Jakuben
Treehouse TeacherAdrian, can you paste in all your code from MainActivity.java? Does the answer appear at all for you?
Adrian Thomas
1,043 PointsSure Ben :D
package com.atDesign.crystalball;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.atDesign.crystalball.R;
import com.atDesign.crystalball.ShakeDetector.OnShakeListener;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getName();
private CrystalBall mCrystalBall = new CrystalBall();
private TextView mAnswerLabel;
private ImageView mCrystalBallImage;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeDetector mShakeDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assign the Views from the layout file
mAnswerLabel = (TextView) findViewById(R.id.textView1);
mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);
//System Services and the Accelerometer
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mShakeDetector = new ShakeDetector(new OnShakeListener() {
@Override
public void onShake() {
handleNewAnswer();
}
});
//Toast.makeText(this, "Yay! Our Activity was created!",Toast.LENGTH_LONG).show();
/*Toast welcomeToast = Toast.makeText(this, "Look at me up here!", Toast.LENGTH_LONG);
welcomeToast.setGravity(Gravity.TOP, 0, 0);
welcomeToast.show();*/
Log.d("TAG", "We're logging from the onCreate() method!");
}
@Override
public void onResume(){
super.onResume();
mSensorManager.registerListener(mShakeDetector, mAccelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onPause(){
super.onPause();
mSensorManager.unregisterListener(mShakeDetector);
}
private void animateCrystalBall() {
mCrystalBallImage.setImageResource(R.drawable.ball_animation);
AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable();
if (ballAnimation.isRunning()){
ballAnimation.stop();
}
ballAnimation.start();
}
private void animateAnswer(){
AlphaAnimation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mAnswerLabel.setAnimation(fadeInAnimation);
}
private void playSound(){
MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private String handleNewAnswer() {
String answer = mCrystalBall.getAnAnswer();
animateCrystalBall();
animateAnswer();
playSound();
return answer;
}
}
Things were grate until the part where we got the device to shake, and then the text would appear. On my phone, I can run the app, and the shake works, just no text. I will say that there have been some errors, and I have resolved them with the inttellisense suggestions. Still, all of this was after text weren't appearing anyway.
My developer troubleshooting skills need work lol.
Ben Jakuben
Treehouse TeacherAny chance this StackOverflow answer is related to your issue? http://stackoverflow.com/questions/11386953/alphaanimation-does-not-work/11387049#11387049
I was able to use your code in my own project. Can you paste in your activity_main.xml code?
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherAlfred, I wonder if it is a timing issue? The fade in animation is set to run for 1.5 seconds: (
fadeInAnimation.setDuration(1500);)What happens if you change that to a shorter time interval, like 500? You should still be able to notice the animation. Perhaps there is a bug if the animation is started too quickly if the previous one is still playing.