Here is a simple class to animate a textview just like a Typewriter.
Typewriter
package typ_tv.coderzheaven.com.typewritertextview; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; public class Typewriter extends TextView { private static final String TAG = Typewriter.class.getSimpleName(); private CharSequence mText; private int mIndex; private long mDelay = 500; // Default 500ms character delay Handler animationCompleteCallBack; public Typewriter(Context context) { super(context); } public void setAnimationCompleteListener(Handler animationCompleteCallBack) { this.animationCompleteCallBack = animationCompleteCallBack; } public Typewriter(Context context, AttributeSet attrs) { super(context, attrs); } private Handler mHandler = new Handler(); private Runnable characterAdder = new Runnable() { @Override public void run() { setText(mText.subSequence(0, mIndex++)); if (mIndex <= mText.length()) { mHandler.postDelayed(characterAdder, mDelay); } else { if (null != animationCompleteCallBack) animationCompleteCallBack.sendMessage(new Message()); else Log.i(TAG, "Animation Complete Listener not set..."); } } }; public void animateText(CharSequence text) { mText = text; mIndex = 0; setText(""); mHandler.removeCallbacks(characterAdder); mHandler.postDelayed(characterAdder, mDelay); } public void setCharacterDelay(long millis) { mDelay = millis; } }
Usage
package typ_tv.coderzheaven.com.typewritertextview; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Handler animationCompleteCallBack = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { Log.i("Log", "Animation Completed"); return false; } }); Typewriter typewriter = new Typewriter(this); typewriter.setCharacterDelay(100); typewriter.setTextSize(30); typewriter.setTypeface(null, Typeface.BOLD); typewriter.setPadding(20, 20, 20, 20); typewriter.setAnimationCompleteListener(animationCompleteCallBack); typewriter.animateText("CoderzHeaven\nHeaven of all working codes"); setContentView(typewriter); } }
Please leave your valuable comments @coderzheaven@gmail.com