title: How to add CrossFade effect in Textview, Android
body: <p>Below code gets a number from two arrays planet_array1 and planet_array, it also randomize the string. Now how can take those two string and add crossfade or fadein effects? Also this code does randomize but stops. How can i add duration that it changes the string automatically after few seconds? i dont want to add any button to change the string.</p>
  
  <pre><code>package com.example.androidanimation;
  
  import java.util.Random;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.view.View;
  import android.view.animation.Animation;
  import android.view.animation.AnimationUtils;
  import android.view.animation.Animation.AnimationListener;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class FadeInActivity extends Activity {
  
  
          String[] mTestArray;
          String[] mTestArray1;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.sample);
  
              mTestArray =   getResources().getStringArray(R.array.planets_array);
              mTestArray1 =   getResources().getStringArray(R.array.planets1_array);
          }
  
          @Override
          protected void onStart() {
              super.onStart();
              updateTextView();    
          }
  
          private void updateTextView() {
              TextView textView = (TextView)findViewById(R.id.randomTextView); 
              TextView textView1 = (TextView)findViewById(R.id.randomTextView1); 
              Random random = new Random();
  
              int maxIndex = mTestArray.length;
              int generatedIndex = random.nextInt(maxIndex);
              textView.setText(mTestArray[generatedIndex]);   
              textView1.setText(mTestArray1[generatedIndex]);  
          }
      }
  </code></pre>
  
  <p>sample.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" &gt;
  
      &lt;TextView
              android:id="@+id/randomTextView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"/&gt;
  
      &lt;TextView
          android:id="@+id/randomTextView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="163dp" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>planet_array</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;string-array name="planets_array"&gt;
          &lt;item&gt;Mercury&lt;/item&gt;
          &lt;item&gt;Venus&lt;/item&gt;
          &lt;item&gt;Earth&lt;/item&gt;
          &lt;item&gt;Mars&lt;/item&gt;
      &lt;/string-array&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>planet1_array</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;string-array name="planets1_array"&gt;
          &lt;item&gt;Merc&lt;/item&gt;
          &lt;item&gt;Ven&lt;/item&gt;
          &lt;item&gt;Ear&lt;/item&gt;
          &lt;item&gt;Mar&lt;/item&gt;
      &lt;/string-array&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>Final working output</p>
  
  <pre><code>    public class MainActivity extends FragmentActivity {
  
      private TextSwitcher mSwitcher, mSwitcher1;
      private int mCounter = 0;
      String textToShow[] = {
              "Main HeadLine", "Your Message", "New In Technology", "New Articles", "Business News", "What IS New"
      };
      String textToShow1[] = {
              "Main HeadLine", "Your Message", "New In Technology", "New Articles", "Business News", "What IS New"
      };
      int messageCount = textToShow.length;
      // to keep current Index of text
      int currentIndex = -1;
  
      Timer timer = new Timer();
  
      public class MyTimerTask extends TimerTask {
  
          @Override
          public void run() {
              runOnUiThread(new Runnable() {
  
                  @Override
                  public void run() {
                      updateTextView();
                  }
              });
          }
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.example_layout);
  
          mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
          mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher01);
          // BEGIN_INCLUDE(setup)
          // Set the factory used to create TextViews to switch between.
          mSwitcher.setFactory(mFactory);
          mSwitcher1.setFactory(mFactory);
          Animation in = AnimationUtils.loadAnimation(this,
                  android.R.anim.fade_in);
          Animation out = AnimationUtils.loadAnimation(this,
                  android.R.anim.fade_out);
          mSwitcher.setInAnimation(in);
          mSwitcher1.setInAnimation(in);
          mSwitcher.setOutAnimation(out);
          mSwitcher1.setOutAnimation(out);
  
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          timer.schedule(new MyTimerTask(), 0, 1000);
  
      }
  
      private void updateTextView() {
  
          int maxIndex = textToShow.length;
          Random random = new Random();
          int generatedIndex = random.nextInt(maxIndex);
          mSwitcher.setText(textToShow[generatedIndex]);
          mSwitcher1.setText(textToShow1[generatedIndex]);
      }
  
      private ViewFactory mFactory = new ViewFactory() {
  
          @Override
          public View makeView() {
  
              // Create a new TextView
              TextView t = new TextView(MainActivity.this);
              t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
              t.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
              return t;
          }
      };
  
  }
  </code></pre>