TextView keeps popping up - android

I have an activity where an TextView show up, and after that it fades out. However, everytime it has fade out, it pops back in. I tried
wtext.setText("");
But then my text just disappeares without fading out(after fading in). Does anyone knows how to fix this? Thanks! Kind regards.
This is my code:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
final TextView wtext = (TextView) (findViewById(R.id.wtext));
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
#Override
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
wtext.setText("");
}
};
handler.postDelayed(task, 5000);
}

If you want something to happen after an animation is complete (in this case, hide a textview), you'll have to use an AnimationListener
Animation.AnimationListener myListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wtext.setVisibility(View.INVISIBLE);
}
};
animation1.setAnimationListener(myListener);

You could use a ViewPropertyAnimator, which will change the view's actual properties rather than just its appearance, so that it will not jump back to the original state after the animation is complete.
wtext.animate().alpha(0);

You could try setting fillAfter flag to true on the element: it will apply the final transformation to it after the animation is finished.

Related

Android .cancel() doesn't cancel the animation

I'm trying to cancel an animation with a very simply way just to see whether if .cancel() function doing what it is intended to do.
But it seems it just does not work.
Here is the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.my_text_view);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_animation);
textView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animation.cancel();
}
}, 2000);
}
}
The animation is running forever. It just doesn't stop after 2 seconds.
If I call it from the view:
textView.getAnimation().cancel();
It doesn't work either.
What do you think what makes this? I need a proper way to stop an animation.

How to go to an anther activity after the end of a frame by frame android animation

How to start an activity after the animation has ended. I have added android:oneshot="true" in the xml but how to start a new activity after this animation has stopped.I have attached the entire code below. Please let me know how to start new activity.If I use this code without showing my animation it goes to the another acitivity.
public class MainActivity extends Activity {
ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animation = (AnimationDrawable) iv.getBackground();
if(hasFocus) {
animation.start();
startActivity(new Intent(MainActivity.this,second.class));
} else {
animation.stop();
}
}
public void onStart() {
{
super.onStart();
iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.animator.animation);
}
}
}
One way for sure you can use onAnimationListener, and be really interested into implementing the onAnimationEnd method of the listener interface.
Something like this:
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(MainActivity.this,second.class));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Android animation for 1 second

I am trying to make a page where when the user leaves the page, there will be an animation (eg.: button will slide out) in the page and after that, the user will be sendt to a different activity.
No issue with the animation, but as the code for starting the new activity is written just after the animation code, the animation is not completing for 1 second (as I have set).
I want to execute the animation for 1 second first and then move to another activity.
Please help me.
Use AnimationListener.
private Animation.AnimationListener animListener = new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
// write code to start new activity.
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
};
Assign above listener to your animation
animation.setAnimationListener(animListener);
//Startanimation
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// call Activity
// End animation
}
}, 1000);

android:null pointer at splash screen

I am making an app in which i have to implement alpha effect in splash screen and when i load image it gives null pointer exception.The basic Problem while starting animation.If i remove start animation then my animation do not start at all.I am really stuck.Any help will be appreciated.My code is as follows:
public class SplashScreen extends Activity {
private Thread mSplashThread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("hello");
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
Animation a1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
LinearLayout Ll=(LinearLayout)findViewById(R.id.mainLayoutheader);
System.out.println("hello1");
Ll.startAnimation(a1);
System.out.println("hello2");
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
// a.reset();
wait(6000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, Main.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
}
Exception is at L1.startAnimation(a1);
I guess you forgot to call setContentView. You have to do that before calling findViewById [edit: doesn't solve the problem here]
You can try timer thread...use this link
http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/
Ll is null or a1 is null and startAnimation throws the exception. The full stack trace shows, which one threw it.
Also:
You might have accidentally typed R.id.mainLayoutheader instead of R.id.mainLayoutHeader.
Check the name R.anim.alpha is correct.
The problem was that the view in which i was showing image was empty.
super.onCreate(savedInstanceState);
// Splash screen view
System.out.println("hello");
setContentView(R.layout.splash);
ImageView iv=(ImageView)findViewById(R.id.splashscreen12);
Animation scaleAnim = AnimationUtils.loadAnimation(this,R.anim.alpha);
iv.startAnimation(scaleAnim);
scaleAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
// Start new activity after some delay
TimerTask newActivity = new TimerTask() {
#Override
public void run() {
startActivity(new Intent(SplashScreeen.this,CallingActivity.class));
SplashScreeen.this.finish();
}
};
Timer t = new Timer(false);
t.schedule(newActivity, 2500);
}
#Override
public void onAnimationRepeat(Animation animation) {
// DO NOTHING
}
#Override
public void onAnimationStart(Animation animation) {
// DO NOTHING
}
});

OnClickListener not firing until onResume()

I've got an activity in which several Views animate. The animations are triggered when a RelativeLayout containing a few images is clicked. It's set up in onCreate() like this:
mContainer.setOnClickListener(new ContainerClicked());
And the ContainerClicked() class looks like this:
private class ContainerClicked implements OnClickListener {
#Override
public void onClick(View arg0) {
Log.i(TAG, "TEST!");
mSomeButton.setOnClickListener(null);
mSomeButton.setEnabled(false);
mAnotherButton.setOnClickListener(null);
mAnotherButton.setEnabled(false);
mYetAnotherButton.setOnClickListener(null);
mYetAnotherButton.setEnabled(false);
mContainer.setOnClickListener(null);
if (mLogo.getVisibility() == View.VISIBLE)
new AnimateToParty().execute();
else
new AnimateFromParty().execute();
}
}
This works, and animates everything just how I want it.
AnimateToParty() looks like this:
private class AnimateToParty extends AsyncTask<Void, Integer, Void> {
#Override
protected void onProgressUpdate(final Integer... values) {
final View theView = findViewById(values[0]);
if (!(values[0] == mBackgroundImage.getId() || values[0] == mContainer
.getId())) {
final Animation animation = AnimationUtils.loadAnimation(
DashboardActivity.this, values[1]);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
theView.setVisibility(View.INVISIBLE);
}
});
theView.startAnimation(animation);
} else if (values[0] == mBackgroundImage.getId()) {
TransitionDrawable transition = (TransitionDrawable) theView
.getBackground();
transition.startTransition(getResources().getInteger(
android.R.integer.config_mediumAnimTime));
} else {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
-60, getResources().getDisplayMetrics()));
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
LayoutParams lp = (LayoutParams) mContainer
.getLayoutParams();
lp.bottomMargin = 0;
RelativeLayout parent = (RelativeLayout) mContainer
.getParent();
mSomeButton.setVisibility(View.GONE);
mAnotherButton.setVisibility(View.GONE);
mYetAnotherButton.setVisibility(View.GONE);
mLogo.setVisibility(View.GONE);
// mContainer.setLayoutParams(lp);
// This caused a visible flicker, so I went with the solution below.
parent.removeView(mContainer);
parent.addView(mContainer, lp);
}
});
animation.setDuration(1000);
mContainer.startAnimation(animation);
}
}
#Override
protected Void doInBackground(Void... arg0) {
try {
publishProgress(mContainer.getId());
publishProgress(mLogo.getId(), R.anim.slide_out_up);
Thread.sleep(100);
publishProgress(mSomeButton.getId(), R.anim.slide_out_left);
Thread.sleep(110);
publishProgress(mAnotherButton.getId(), R.anim.slide_out_left);
Thread.sleep(120);
publishProgress(mYetAnotherButton.getId(), R.anim.slide_out_left);
Thread.sleep(130);
publishProgress(mBackgroundImage.getId());
Thread.sleep(550);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
mContainer.setOnClickListener(new LighterClicked());
}
}
The class above animates some buttons and the logo out of the way, and then reveals the container. In order to keep it there, I change it's layoutparams. Just changing them produces a visible flicker where the container jumps 60 dip up for about one frame (the animation doesn't seem to be quite finished), before settling where I want it. I read somewhere to remove it from its parent and put it back in, and this does not produce the same flicker. However, now the rebinding of the onclicklistener does not work!
I've tried just about everything, and I just now noticed that every click that didn't "go through" fires if I press the home button and open the application again.
Is there a better way to avoid the "flicker"? A better way to do the animations? (I'm quite new at animating views) Why do the click-events fire when I reopen the application?
To avoid the flicker you are seeing, you have to call clearAnimation() at the start of onAnimationEnd
#Override
public void onAnimationEnd(Animation arg0) {
theView.clearAnimation()
...
}

Categories

Resources