Smooth text change in android animation

Use a TextSwitcher

<TextSwitcher
  android:layout_marginTop="50dp"
  android:id="@+id/textSwitcher"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal">
    <TextView 
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
    <TextView 
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
</TextSwitcher>

Array of Strings to show in TextSwitcher

String textToShow[] = {"Main HeadLine", "Your Message", "New In Technology"};

You have to set animation

mSwitcher.setInAnimation(context, android.R.anim.slide_in_left);
mSwitcher.setOutAnimation(context, android.R.anim.slide_out_right);

Animation is triggered by calling method setText(CharSequence text)

// When clicked on Button TextSwitcher will switch between texts
btnNext.setOnClickListener(new View.OnClickListener() {
    public void onClick (View v) {
    
       currentIndex++;
       // If index reaches maximum reset it
       if (currentIndex == textToShow.length) {
          currentIndex = 0;
       }

       mSwitcher.setText(textToShow[currentIndex]);
}});

If you want to set text without animation, call method setCurrentText(CharSequence text).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)