'Java typewriter effect

Hello I want to have a typewrite effect on the screen where every few seconds a letter appear after the previous one. I was thinking about having a string with all the text I want to write and every second I would take the first char of that string, remove it and then add it to a different string. For example:

  String text = "hello world";
  String onscreenText = "";

Then onscreenText would have "h" in it and text would have everything but the "h" in it and so on. How can I remove the first char from a string and add it to the next string?



Solution 1:[1]

If you want to reduce your String you can call: text = text.substring(1,text.length) but of course only when there is more than one char inside. The rest with thread delay was already explained by the other posters. Perhaps you can also have a look at Timer class to schedule delayed tasks.

Solution 2:[2]

Super Simple to and easy. Don't pay attention to the extended TextView class created. Lets make it simple and easy for everyone. Check it out. First create a class called Typewriter. Then copy and paste this code in that class. close the class.

import android.os.Handler;
import android.widget.TextView;

public class Typewriter {

    private String sText = new String();
    private int index;
    private long mDelay = 100;

    TextView textView;

    public Typewriter(TextView tView) {
        textView = tView;
    }

    public void animateText(String string) {
        sText = string;
        index = 0;

        textView.setText("");

        new Handler().removeCallbacks(characterAdder);
        new Handler().postDelayed(characterAdder, mDelay);
     }

    private Runnable characterAdder = new Runnable() {
        @Override
        public void run() {
            textView.setText(sText.subSequence(0, index++));

            if (index <= sText.length()) {
                new Handler().postDelayed(characterAdder, mDelay);
            }
        }
    };
}

Now after you created that class the new Class that you want to implement this animation feature to is super simple. Lets say for example you want to add it new a class called MainActivity.

import com.utilities.Typewriter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private Typewriter writer;

    private TextView tv_textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv_textView = (TextView) findViewById(R.id.tv_textView);
        writer = new Typewriter(tv_textView);
        writer.animateText("Super Simple Animation");
    }
}

Thats it and your done. Super Simple leave me votes thanks again.

Solution 3:[3]

Check this out I am using this

class TypingBot extends Thread{
    String textToAnimate;
    int delayInMilliSeconds;
    public TypingBot(String s, int delayInMilliSeconds){
        this.textToAnimate = s;
        this.delayInMilliSeconds = delayInMilliSeconds;
    }
    @Override
    public void run(){
        for (int i = 0 ; i < textToAnimate.length() ; i++){
            System.out.print(textToAnimate.charAt(i));
            try{
                Thread.sleep(delayInMilliSeconds);
            }
            catch (Exception e){
                System.out.println(e);
            }
        }
    }
}

public class TypingAnimation {
    public static void startAnimation(String textToAnimate, int waitForMillis){
        TypingBot typingBot = new TypingBot(textToAnimate, waitForMillis);
        typingBot.isDaemon();
        typingBot.start();

        Thread currentThread = Thread.currentThread();
        try{
            currentThread.join(waitForMillis);
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
    public static void main(String[] args){
        startAnimation("Krishna Wadhwani Is The Best Programmer", 500);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henning Luther
Solution 2 user3587194
Solution 3 Krishna Wadhwani