YoShade
YoShade

Reputation: 209

How to fade in textview letter by letter in Android

I am trying to do a fade in animation on a textview letter by letter but when i do it with view animation it fade in the entire textview.

I tried to combine view animation with a handler for when letter appear but i don't get the result i wanted. this is the code i tried. the xml file

<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha 
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="3000"
    android:fillBefore="true"
    android:zAdjustment="bottom"/>

the java code

public class TextViewAnimator {
private TextView textView;
private CharSequence text;
private long delay;
private int index;
private Handler timerHandler = new Handler();
private Runnable animationTask = new Runnable() {
    @Override
    public void run() {
        textView.setText(text.subSequence(0, index++));
        if (index <= text.length()) {
            timerHandler.postDelayed(animationTask, delay);
        }
    }
};

public static TextViewAnimator newInstance(TextView textView,
        CharSequence text, long delay) {
    TextViewAnimator instance = new TextViewAnimator();
    instance.textView = textView;
    instance.text = text;
    instance.delay = delay;
    return instance;
}

public void start() {
    textView.setText("");
    timerHandler.postDelayed(animationTask, delay);
}

}

is there another way to get fade in letter by letter?

Upvotes: 2

Views: 1240

Answers (1)

cht
cht

Reputation: 319

I guess this is too late but still may help someone. I too needed something like this but found a satisfactory solution nowhere, decided to make a custom View of my own, then transformed it into a library. Try [Fade-In TextView][1]. It sets text letter by letter with a nice fade-in animation.

Usage

In the XML layout

    <believe.cht.fadeintextview.TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textColor="@android:color/black"

        app:letterDuration="250"/>

And in the Activity/Fragment

believe.cht.fadeintextview.TextView textView = (believe.cht.fadeintextview.TextView) findViewById(R.id.textView);

textView.setLetterDuration(250); // sets letter duration programmatically
textView.isAnimating(); // returns current animation state (boolean)
textView.setText(); // sets the text with animation

Upvotes: 1

Related Questions