Created
September 1, 2017 19:11
-
-
Save Binary-Finery/a6627c1b4461d6d49c78c9de2ccf8cee to your computer and use it in GitHub Desktop.
Type writer style append text view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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