Search code examples
androidtextview

Print with delay a string in a TextView


I am trying to print a String in a TextView on my AndroidApp with some delay between each character. The String is pi and I want to simulate the fact that the app is computing it live. I tried to add some delay with a sleep function so as not to flood the TextView with too many request too quickly, to no avail as it still doesn't work :(

The string is: String pi_value = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117";

public void printWithDelays(String data, TimeUnit unit, long delay)
            throws InterruptedException {
        String pi_temp = "";
        TextView pi_text_holder = findViewById(R.id.pi_text_holder);
        pi_text_holder.setText(pi_temp);
        for (char ch : data.toCharArray()) {
            pi_temp.concat(String.valueOf(ch));
            pi_text_holder.setText(pi_temp);
            unit.sleep(delay);
        }
    }

If you had an idea I would be very grateful.


Solution

  • This is a pretty good solution. It works well.

    https://ssaurel.medium.com/create-a-type-writer-effect-on-android-4dc8f2ccada6