22

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);
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
DeepakKUMARYadav
  • 223
  • 1
  • 2
  • 4

5 Answers5

20

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/

AI Shakil
  • 1,015
  • 10
  • 20
Raza Ali Poonja
  • 1,086
  • 8
  • 16
  • I would recommend also using an interpolator, because else the animation will just jump from size 1 to 2 after 3 seconds. – Marie M. Jan 13 '21 at 11:55
4

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();
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
  • can i set the animation speed of this Textview? Like i am using animation in my splash screen and my text is animated but its animating very fast. – Sunny Mar 20 '20 at 03:48
2

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

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • Based on comment in other answer - no, it is not correct: "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setAnimation(android.view.animation.Animation)' on a null" this should be the accepted answer – wfranczyk Dec 04 '15 at 13:31
0

You need setAnimation in your TextView

Example:

tv.setAnimation( animation ); 
dpulgarin
  • 556
  • 5
  • 17
  • i have tried that also..but still it is saying the same thing Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setAnimation(android.view.animation.Animation)' on a null – DeepakKUMARYadav Dec 04 '15 at 13:13
0

Use Animator/AnimatorSet Animation is legacy code

Duna
  • 1,564
  • 1
  • 16
  • 36