Skip to content

Instantly share code, notes, and snippets.

@VijayMakwana
Last active January 6, 2017 07:23
Show Gist options
  • Save VijayMakwana/5fa3c45068cbca2ef5f453a8ac0f532f to your computer and use it in GitHub Desktop.
Save VijayMakwana/5fa3c45068cbca2ef5f453a8ac0f532f to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.textviewanimation.MainActivity">
<com.textviewanimation.AnimatedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:animText="Hello vijay"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="30sp"
app:charDelay="300"/>
</LinearLayout>
package com.textviewanimation;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by vijay on 06-Jan-17.
*/
public class AnimatedTextView extends TextView {
private static final String TAG = "AnimatedTextView";
private CharSequence mText;
private int mIndex;
private long mDelay = 500; //Default 500ms delay
private boolean flag = false;
private boolean repeat;
public AnimatedTextView(Context context) {
super(context);
}
public AnimatedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
animateText(context, attrs);
setCharacterDelay(context, attrs);
setRepeat(context, attrs);
}
private void setCharacterDelay(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ANIMTextView);
int charDelay = a.getInt(R.styleable.ANIMTextView_charDelay, 500);
a.recycle();
setCharacterDelay(charDelay);
}
private void animateText(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.ANIMTextView);
String animText = a.getString(R.styleable.ANIMTextView_animText);
a.recycle();
animateText(animText);
}
private void setRepeat(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ANIMTextView);
boolean repeat = a.getBoolean(R.styleable.ANIMTextView_repeat, false);
a.recycle();
setRepeat(repeat);
}
public AnimatedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
animateText(context, attrs);
setCharacterDelay(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 (repeat) {
animateText(mText);
flag = true;
}
}
};
public void animateText(CharSequence text) {
if (!flag) {
mText = text + " ";
} else {
mText = text;
}
mIndex = 0;
setText("");
mHandler.removeCallbacks(characterAdder);
mHandler.postDelayed(characterAdder, mDelay);
}
public void setCharacterDelay(long millis) {
mDelay = millis;
}
public void setRepeat(boolean repeat) {
this.repeat = repeat;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ANIMTextView">
<attr name="animText" format="string"/>
<attr name="charDelay" format="integer"/>
<attr name="repeat" format="boolean"/>
</declare-styleable>
</resources>
package com.textviewanimation;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
@VijayMakwana
Copy link
Author

This is the code about animating the text view text are displayed with delayed and display one by one character and also you can add delay time as you want. As well as you can make text repeatable mode then you can add repeat true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment