Introduction

This article explains TextSwitcher and ImageSwitcher in Android. Android Studio is used to create the sample.
TextSwitcher and ImageSwitcher are used for simple animated transitions. TextSwitcher and ImageSwitcher are used for smooth transition animation in Android. For example, changing a number in a DatePicker, CountDown timer in a clock. Text Switcher and ImageSwittcher are the classes in Android.
Step 1
Create a project like this:
textSwitcherproject
Step 2
Create an XML file and with the following.
In this, you will use a TextSwitcher and an ImageSwitcher.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5. <ImageSwitcher
  6. android:id="@+id/switcherImage"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:layout_alignParentLeft="true" >
  10. </ImageSwitcher>
  11. <TextSwitcher
  12. android:id="@+id/switcherText"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentBottom="true"
  16. android:layout_centerHorizontal="true"
  17. android:background="#99000000"
  18. android:padding="10dp" />
  19. <Button
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignParentRight="true"
  23. android:layout_alignParentTop="true"
  24. android:layout_marginTop="10dp"
  25. android:onClick="onSwitch"
  26. android:text="Next Image >>" />
  27. </RelativeLayout>
Step 3
Create a Java class file with the following.
In a Java class file, you will create the id of the TextSwitcher and ImageSwitcher. Now call the setFactory method and the view as an argument.
  1. package com.textswitcher;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.View;
  6. import android.widget.ImageSwitcher;
  7. import android.widget.ImageView;
  8. import android.widget.TextSwitcher;
  9. import android.widget.TextView;
  10. import android.widget.ViewSwitcher.ViewFactory;
  11. public class MainActivity extends Activity
  12. {
  13. private static final String[] TEXT =
  14. {
  15. "Image 1",
  16. "Image 2",
  17. "Image 3"
  18. };
  19. private static final int[] IMAGES =
  20. {
  21. R.drawable.image1,
  22. R.drawable.images2,
  23. R.drawable.images3
  24. };
  25. private int Position = 0;
  26. private TextSwitcher switcherText;
  27. private ImageSwitcher switcherImage;
  28. @Override
  29. public void onCreate(Bundle savedInstanceState)
  30. {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. switcherText = (TextSwitcher) findViewById(R.id.switcherText);
  34. switcherText.setFactory(new ViewFactory()
  35. {
  36. @Override
  37. public View makeView()
  38. {
  39. TextView textView = new TextView(MainActivity.this);
  40. textView.setGravity(Gravity.CENTER);
  41. return textView;
  42. }
  43. });
  44. switcherText.setInAnimation(this, android.R.anim.fade_in);
  45. switcherText.setOutAnimation(this, android.R.anim.fade_out);
  46. switcherImage = (ImageSwitcher) findViewById(R.id.switcherImage);
  47. switcherImage.setFactory(new ViewFactory()
  48. {
  49. @Override
  50. public View makeView()
  51. {
  52. ImageView imageView = new ImageView(MainActivity.this);
  53. return imageView;
  54. }
  55. });
  56. switcherImage.setInAnimation(this, android.R.anim.slide_in_left);
  57. switcherImage.setOutAnimation(this, android.R.anim.slide_out_right);
  58. onSwitch(null);
  59. }
  60. public void onSwitch(View view)
  61. {
  62. switcherText.setText(TEXT[Position]);
  63. switcherImage.setBackgroundResource(IMAGES[Position]);
  64. Position = (Position + 1) % TEXT.length;
  65. }
  66. }
Step 4
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.textswitcher"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.textswitcher.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
ImageSwitch1
textswitcher2
textswitcher3