Android character by character display text animation
This may not be the most elegant solution, but the simplest is probably a quick subclass of TextView
with a Handler
that updates the text every so often until the complete sequence is displayed:
public class Typewriter extends TextView {
private CharSequence mText;
private int mIndex;
private long mDelay = 500; //Default 500ms delay
public Typewriter(Context context) {
super(context);
}
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);
}
}
};
public void animateText(CharSequence text) {
mText = text;
mIndex = 0;
setText("");
mHandler.removeCallbacks(characterAdder);
mHandler.postDelayed(characterAdder, mDelay);
}
public void setCharacterDelay(long millis) {
mDelay = millis;
}
}
You can then use this in an Activity like so:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Typewriter writer = new Typewriter(this);
setContentView(writer);
//Add a character every 150ms
writer.setCharacterDelay(150);
writer.animateText("Sample String");
}
}
If you want some animation effects with each letter added, perhaps look at subclassing TextSwitcher
instead.
Hope that Helps!
Trouble with Android character by character display text animation
You are using it in a wrong way. To make your textview like in example do this.
Typewriter contact_popup = (Typewriter) view.findViewById(R.id.contact_popup);
contact_popup.setCharacterDelay(150);
contact_popup.animateText("Sample String");
return view;
Display a String letter by letter in a TextView -Android
This code works,
public void setText(final String s)
{
TextView tv= (TextView)HomeActivity.tf.getView().findViewById(R.id.textViewFragment);
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]);
Log.d("Strange",""+c);
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);
}
How to animate TextView with Alpha from Left to Right?
After doing lots of research on the same, I'm posting my own answer.
Steps:
Create CustomTextLayout
class CustomTextLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayoutCompat(context, attrs, defStyleAttr) {
private var characterAnimationTime = 100
private var textSize = 22f
private var letterSpacing = 0f
private var animationDuration = 2000L
init {
orientation = HORIZONTAL
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextLayout, defStyleAttr, 0)
textSize = typedArray.getFloat(R.styleable.CustomTextLayout_textSize, textSize)
typedArray.recycle()
}
/**
* This function sets the animated alpha text
* @param context Context of Activity / Fragment
* @param text Text string
* @param initialDelay Start animation delay
*/
fun setAnimatedText(context: Context, text: String, initialDelay: Long = 0) {
var textDrawPosition = 0
Handler().postDelayed({
for (char in text) {
val textView = getTextView(char.toString())
textView.visibility = View.GONE
this.addView(textView)
textDrawPosition++
drawAnimatedText(
context,
this,
textView,
textDrawPosition,
text,
(textDrawPosition * characterAnimationTime).toLong()
)
}
}, initialDelay)
}
private fun drawAnimatedText(
context: Context,
parentView: LinearLayoutCompat,
textView: AppCompatTextView,
position: Int,
text: String,
initialDelay: Long
) {
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), Color.WHITE, Color.BLACK)
colorAnimation.startDelay = initialDelay
colorAnimation.duration = animationDuration
colorAnimation.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animator: Animator) {
textView.visibility = View.VISIBLE
}
override fun onAnimationEnd(animator: Animator) {
if (position == text.length) {
val updatedTextView = getTextView(text)
updatedTextView.setTextColor(Color.BLACK)
updatedTextView.visibility = View.VISIBLE
parentView.removeAllViews()
parentView.addView(updatedTextView)
}
}
override fun onAnimationCancel(animator: Animator) {
}
override fun onAnimationRepeat(animator: Animator) {
}
})
colorAnimation.addUpdateListener {
textView.setTextColor(it.animatedValue as Int)
}
colorAnimation.start()
}
private fun getTextView(text: String): AppCompatTextView {
val textView = AppCompatTextView(context)
textView.text = text
textView.textSize = textSize
textView.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC)
textView.letterSpacing = letterSpacing
return textView
}Add in layout file
<com.mypackagename.CustomTextLayout
app:textSize="30"
app:letterSpacing="0.1"
android:id="@+id/textLayoutFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</com.mypackagename.CustomTextLayout>Add attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTextLayout">
<attr name="textSize" format="float"/>
<attr name="letterSpacing" format="float"/>
</declare-styleable>
</resources>Start animation:
textLayoutFirst.setAnimatedText(this, "Some text here")
It's done.
animate text letters
found an easy way by using two TextView controls. First is set to the first latter of a word by mytext.substring(0,1) and second one to mytext.substring(1).
when do startAnimation for both or you can have one animation for the first view and another one for the second one.
on layout file, i put them next to each other on RelativeLayout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txtCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80dp"
android:textColor="#FFFFFF"
android:text="est"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<TextView
android:id="@+id/txtCaptionFirstLetter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="70dp"
android:textColor="#00FF00"
android:textStyle="bold"
android:text="T"
android:layout_toLeftOf="@+id/txtImageCaption"
android:layout_alignBottom="@+id/txtImageCaption"
/>
</RelativeLayout>
Changing Text color animation in Android
You can use a TextSwitcher
See these tutorials / examples. They will help you get started:
- http://www.example8.com/category/view/id/15552
- http://www.coderzheaven.com/2011/07/02/textswitching-animation-in-android-or-textswitcher-control-in-android/
- http://www.java2s.com/Code/Android/UI/UsesaTextSwitcher.htm
And finally, I believe an example is also available in the API demos from the Samples For SDK (API 13 I think) package in the SDK Manager
You will naturally have to code for synchronizing the TTS speech and the animation itself.
Related Topics
Programmatically Set Android Phone's Background
How to Move Bluetooth Activity into a Service
Best Practices: Layouts on Android (Programmatic VS Xml)
Findviewbyid() Returns Null When I Call It in Oncreate()
Simulate Low Battery & Low Memory in Android
Locationlistener of Network_Provider Is Enabled But , Onlocationchanged Is Never Called
Open Soft Keyboard Programmatically
Google Maps API V2 Draw Part of Circle on Mapfragment
How to Use View Pager with Views (Not with Fragments)
Error :: Duplicate Files During Packaging of APK
Can't Stop the Ringing Alarm from Another Activity