An Android view that can be used to sequentially animate typing and deleting one character at a time. Pausing and enqueuing arbitrary Runnables is also possible. Inspired by Devunwired @ Stackoverflow (see http://stackoverflow.com/questions/6700374/android-character-by-character-display-text-animation).
An EditText view has been used as base to get a blinking cursor. This has the drawback that the user could edit the text while it is animating. Minimum changes to the code are required to instead use a TextView as base if that is an issue.
TypewriterView typewriterView = (TypewriterView) view.findViewById(R.id.typewriter);
typewriterView.setText("Hello ");
typewriterView.pause(3000)
.type("cruel").pause()
.delete("cruel").pause(500)
.type("nice").pause()
.type(" world").pause()
.run(new Runnable() {
@Override
public void run() {
// Finalize the text if user fiddled with it during animation.
typewriterView.setText("Hello nice world");
typewriterView.setEnabled(false);
}
});
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<your.package.TypewriterView
android:id="@+id/tagline_typewriter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textSize="16sp"
android:windowSoftInputMode="stateHidden"
android:textColor="@android:color/white"
android:text="@string/splash_tag_line_begining"/>
<!-- Overlay a view to prevent user from focusing the typewriter edit text view -->
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:editable="false"
android:focusable="false"
android:background="@android:color/transparent"></EditText>
</FrameLayout>
Is it possible to know when the typewriter finished the animation? I know I can calculate with the time-per-character but I'm not sure how or if there's a better way. cheers :)