Last active
March 4, 2021 06:12
-
-
Save Antarix/6388606 to your computer and use it in GitHub Desktop.
TextView animation like type writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Handler; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class TypeWriter extends TextView { | |
private CharSequence mText; | |
private int mIndex; | |
private long mDelay = 150; //Default 150ms delay | |
public TypeWriter(Context context) { | |
super(context); | |
} | |
public TypeWriter(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
private Handler mHandler = new Handler(); | |
private Runnable characterAdder = new Runnable() { | |
@Override | |
public void run() { | |
setText(mText.subSequence(0, mIndex++)); | |
if(mIndex <= mText.length()) { | |
mHandler.postDelayed(characterAdder, mDelay); | |
} | |
} | |
}; | |
public void animateText(CharSequence text) { | |
mText = text; | |
mIndex = 0; | |
setText(""); | |
mHandler.removeCallbacks(characterAdder); | |
mHandler.postDelayed(characterAdder, mDelay); | |
} | |
public void setCharacterDelay(long millis) { | |
mDelay = millis; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Welcome @claudijo