Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth text change in android animation

I want to add animation in android text view so that when a text changes it should change smoothly and slowly. Like, fade in or fade out when the text changes. Is it possible in Android using animations? I did so far;

Main Activity

public class MainActivity extends AppCompatActivity {      TextView tv;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Button btn = (Button) findViewById(R.id.btn);          tv = (TextView)findViewById(R.id.textview);          btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                     tv.setText(String.valueOf(Integer.valueOf((String) tv.getText()) + 1));             }         });     } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:gravity="center_horizontal"     tools:context="com.example.namal.smoothtextchange.MainActivity">      <TextView         android:layout_width="wrap_content"         android:id="@+id/textview"         android:textSize="150sp"          android:layout_height="wrap_content"         android:text="0" />      <Button         android:text="Change"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/textview"         android:id="@+id/btn" /> </RelativeLayout> 
like image 930
Humty Avatar asked Jan 18 '17 09:01

Humty


1 Answers

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).

like image 149
Salauddin Gazi Avatar answered Sep 22 '22 05:09

Salauddin Gazi