DeepakKUMARYadav
DeepakKUMARYadav

Reputation: 223

How to add animation to a text view in android

I have a TextView and I'm trying to add a fade in animation to it. My code is returning null and I don't understand why.

Here is my implementation

This is the fade_in.xml

    <alpha
            xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true"
            android:duration="1000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0"/>

and here is how im using it in the corresponding activity

    tv= (TextView)findViewById(R.id.textView);
//-- the below line is returning null
            animation = AnimationUtils.loadAnimation(this,R.anim.fade_in);

            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                tv.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    Intent it  = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(it);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            tv.startAnimation(animation);

Upvotes: 22

Views: 83135

Answers (5)

Raza Ali Poonja
Raza Ali Poonja

Reputation: 1106

Android TextView Annimation example

XML

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
      android:fromXScale="1.0"
      android:fromYScale="1.0"
      android:toXScale="2.0"
      android:toYScale="2.0"
      android:duration="3000" />
</set>

Code

private void RunAnimation() 
{
  Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
  a.reset();
  TextView tv = (TextView) findViewById(R.id.firstTextView);
  tv.clearAnimation();
  tv.startAnimation(a);
}

For More :

http://chiuki.github.io/advanced-android-textview/#/5

http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/

Upvotes: 20

Kanagalingam
Kanagalingam

Reputation: 2184

You can load animations from AnimationUtils class in Android and set it to a textview in android.

textview.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));

and you can stop animation using,

textview.clearAnimation();

Upvotes: 4

Duna
Duna

Reputation: 1612

Use Animator/AnimatorSet Animation is legacy code

Upvotes: 0

Vivek Mishra
Vivek Mishra

Reputation: 5705

Is your textview id correct?? First check if you are getting your textview id correctly in your app

Upvotes: 2

dpulgarin
dpulgarin

Reputation: 556

You need setAnimation in your TextView

Example:

tv.setAnimation( animation ); 

Upvotes: 0

Related Questions