Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created September 1, 2017 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Binary-Finery/a6627c1b4461d6d49c78c9de2ccf8cee to your computer and use it in GitHub Desktop.
Save Binary-Finery/a6627c1b4461d6d49c78c9de2ccf8cee to your computer and use it in GitHub Desktop.
Type writer style append text view
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Handler handler;
private int idx = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView textView = (TextView) findViewById(R.id.tv);
final String STRING = "Hello Android";
/*pass in 3 params:
* 1) the string you wish to display
* 2) the text view you wish to display the string in
* 3) the delay time for appending the text view with the next character in
* the string...
*/
typeWriter(STRING, textView, 300);
}
public void typeWriter(String string, final TextView textView, final int DELAY) {
final char[] ca = string.toCharArray();
final int max = ca.length;
handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
textView.append(String.format("%c", ca[idx]));
idx++;
if (idx < max) handler.postDelayed(this, DELAY);
}
};
handler.post(runnable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment