Search code examples
androidtextviewandroid-animation

Constantly setting animation on TextView and Constantly changing text on TextView Using “run” Method and Handler


I have a string array that contains the string values, it displays on the TextView after some time one by one. It stops on the last index of the array. I want to start again changing the text of the TextView from index zero to last index.

I have an animation that is the applied to the above mentioned TextView. The animation showing its result only once on starting time, after that it does not showing its result. Here is my Java code.

Animation animMove;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);

    doTask();

    animMove = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.move);
}

public void doTask() {

    final String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
    final android.os.Handler handler = new android.os.Handler();
    handler.post(new Runnable() {

        int i = 0;

        @Override
        public void run() {
            animMove.setRepeatCount(Animation.INFINITE);
            intelStoryTextView.setAnimation(animMove);
            intelStoryTextView.setText(my_string[i++]);

            if (i == my_string.length) {
                i = 0;
                handler.removeCallbacks(this);

            } else {

                handler.postDelayed(this, 1000 * 5);
            }

        }
    });
}

Here is my animation code.

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

    <translate
        android:duration="2000"
        android:fromXDelta="70%p"
        android:toXDelta="0%p"
        />

</set>

Solution

  • Try this code below, i've tested it and it should work fine:

    Your move.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator"
        android:fillAfter="true">
    
        <alpha
            android:duration="2000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0"
            android:repeatCount="infinite"
            android:repeatMode="restart" /> <!-- i've added repeatCount and repeatMode attr -->
    
        <translate
            android:duration="2000"
            android:fromXDelta="70%p"
            android:toXDelta="0%p"
            android:repeatCount="infinite"
            android:repeatMode="restart"/> <!-- i've added repeatCount and repeatMode attr -->
    </set>
    

    Your activity:

        AnimationSet animMove; // i changed Animation to AnimationSet because i need the list of animations
        TextView intelStoryTextView;
        String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
        int position = 0; // a counter for your list of strings
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
    
            animMove = (AnimationSet) AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.move);
    
            // I get an animation from the animations list (i choose randomly the first one)
            Animation alphaAnim = animMove.getAnimations().get(0);
            // I add a listener to this animation to update text when repeating
            alphaAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // here i do the update
                    if (position >= my_string.length) {
                        position = 0;
                    }
    
                    intelStoryTextView.setText(my_string[position]);
                    position++;
                }
            });
    
            // I start the animation
            intelStoryTextView.startAnimation(animMove);
    
        }
    

    Update

    If you want to stop an infinite animation for a while before repeating it, you may use a Handler instead of repeartCount tag:

    move.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator"
        android:fillAfter="true">
    
        <alpha
            android:duration="2000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0" />
    
        <translate
            android:duration="2000"
            android:fromXDelta="70%p"
            android:toXDelta="0%p"/>
    </set>
    

    Your activity

        Animation mAnimMove;
        TextView mIntelStoryTextView;
        String[] mMy_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
        int mPosition = 0;
        int mInterval = 5000;
        Handler mHandler = new Handler();
        Runnable mRunnable;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mIntelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
    
            mAnimMove = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.move);
    
            mRunnable = new Runnable() {
                @Override
                public void run() {
    
                    if (mPosition >= mMy_string.length) {
                        mPosition = 0;
                    }
    
                    mIntelStoryTextView.setText(mMy_string[mPosition]);
                    mPosition++;
                    mIntelStoryTextView.startAnimation(mAnimMove);
    
                    mHandler.postDelayed(mRunnable, mInterval);
                }
            };
    
            mRunnable.run();
        }