Java – How to delay displaying text in a TextView (verbatim)

How to delay displaying text in a TextView (verbatim)… here is a solution to the problem.

How to delay displaying text in a TextView (verbatim)

I want the text in

the TextView to be verbatim, or even letter-by-letter, as it is in most role-playing and adventure games with text boxes. A good example of this is what a text stream should look like in the game phoenix wright ( ).

What I’ve tried so far is:

protected void onCreate(Bundle savedInstanceState) {
    String text = "test test test test";
    String[] split = text.split(" ");
    Deque<String> words = new LinkedList<String>();
    for (int i = 0; i<split.length; i++)


public void showNextWord(final Deque<String> words)
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() 
        public void run() 
            TextView t = (TextView) findViewById(;
            t.append(words.pollFirst()+" ");
            if (words.size()>0)
    }, 500);


tested it on the emulator and it seems to have low performance, and if I start lagging after displaying each letter, the performance is even worse. Latency inconsistency.

Other than that, I would like a more elegant solution. Maybe there are ways to be more flexible about delays? For example. Greater delay after a sentence and so on.

Thank you very much!


public class Typewriter extends TextView {

private CharSequence mText;
private int mIndex;
private long mDelay = 500; Default 500ms delay

public Typewriter(Context context) {

public Typewriter(Context context, AttributeSet attrs) {
    super(context, attrs);

private Handler mHandler = new Handler();
private Runnable characterAdder = new Runnable() {
    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;

    mHandler.postDelayed(characterAdder, mDelay);

public void setCharacterDelay(long millis) {
    mDelay = millis;

Then use the above class in your activity like this:

    Typewriter writer = new Typewriter(this);
    Add a character every 200ms
    writer.animateText("Sample String");

Related Problems and Solutions