I want to do the following:
I give a String to a global function in my App and this function opens a FrameLayout that looks like a speech bubble. This one includes a TextView for the String.
Works pretty well so far - but I want it to look like a "live speech". So the text shouldn't just appear, but every single character after x miliseconds. Like:
H
He
Hel
Hell
(etc.)
Do you have any ideas for that? Thanks!
I've written a class extending AsyncTask
as a quick example of how you can do this:
public class TyperTask extends AsyncTask<Void, String, Void> {
TextView mTextView;
String mMessage;
int mTypingDelay;
TyperTask(TextView textView, String message, int typingDelay) {
this.mTextView = textView;
this.mMessage = message;
this.mTypingDelay = typingDelay;
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < mMessage.length(); i++) {
publishProgress(String.valueOf(mMessage.charAt(i)));
try {
Thread.sleep(mTypingDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(String... typedChar) {
mTextView.append(typedChar[0]);
}
}
Here's how to use it:
TextView myTextView = (TextView) findViewById(R.id.myTextView);
new TyperTask(myTextView, "This is a test message.", 100).execute();