Breaking News

Create a Letter by Letter Animation on TextView in Android

Android Text Animation Example

Letter By Letter Animation on TextView  




For that, Create a project in android studio and then create an anim directory under res directory and create an XML under anim directory.

In this, we create 10 type of animations on textviews.

Create a Text View in activitymain.xml to show letter by letter animation.

activity_main.xml


 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".SampleActivity">    
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:gravity="center_horizontal"
        android:textSize="36sp"
        android:id="@+id/textview3"/>    
 </android.support.constraint.ConstraintLayout>  

Then under MainActivity.java your create a Handler and create a timer loop to show a Letter by Letter animation. 

MainActivity.java

 import android.graphics.Color;  
 import android.os.Handler;  
 import android.os.Message;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 import com.wx.wheelview.widget.WheelView;  
 import com.wx.wheelview.widget.WheelViewDialog;  
 import java.text.SimpleDateFormat;  
 import java.util.ArrayList;  
 import java.util.Calendar;  
 import java.util.Date;  
 import java.util.Locale;  
 import java.util.TimeZone;  
 import java.util.Timer;  
 import java.util.TimerTask;  
 public class SampleActivity extends AppCompatActivity {  
   TextView tv;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_sample2);  
     tv = findViewById(R.id.textview3);  
     setText("Harpreet Studio");  
   }  

   public void setText(final String s)  
   {  
     final int[] i = new int[1];  
     i[0] = 0;  
     final int length = s.length();  
     final Handler handler = new Handler()  
     {  
       @Override  
       public void handleMessage(Message msg) {  
         super.handleMessage(msg);  
         char c= s.charAt(i[0]);  
         tv.append(String.valueOf(c));  
         i[0]++;  
       }  
     };  
     final Timer timer = new Timer();  
     TimerTask taskEverySplitSecond = new TimerTask() {  
       @Override  
       public void run() {  
         handler.sendEmptyMessage(0);  
         if (i[0] == length - 1) {  
           timer.cancel();  
         }  
       }  
     };  
     timer.schedule(taskEverySplitSecond, 1, 500);  
   }  
}

After running, your Letter by Letter animation starts on TextView. Adjust your time accordingly in handler method.


Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments