A TextSwitcher is a specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
To learn Basic of Android Animation go to Android Animation Tutorial.
Two types animations are required for for TextSwitcher to switch between the texts.
1: In Animation: with which Text come in the Screen.
2: Out Animation: with which Text goes out from the Screen.
In this example i will demonstrate you how to work switch between the TextViews.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
2. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken TextSwitcher for switching between texts and a Button to show the next Text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp" > <!-- Text Switcher to switch between texts --> <TextSwitcher android:id="@+id/textSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="50dp" /> <Button android:id="@+id/buttonNext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:text="NEXT" /> </LinearLayout> |
3. Now, add the following code to MainActivity.java , in this activity i am going to show how to switch between the Text. You can add as much Text for switching.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.android_textswitcher_demo; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Gravity; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends ActionBarActivity { private static Button next; private static TextSwitcher textswitcher; // String array to be shown on textSwitcher String textToShow[] = { "What is your name?", "I am a developer.", "What are you doing?", "Hey bro, whats up?", "Etc. etc..." }; // Total length of the string array int messageCount = textToShow.length; // to keep current Index of text int currentIndex = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Call all the methods init(); loadAnimations(); setFactory(); setListener(); } void init() { next = (Button) findViewById(R.id.buttonNext); textswitcher = (TextSwitcher) findViewById(R.id.textSwitcher); } void loadAnimations() { // Declare the in and out animations and initialize them Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); // set the animation type of textSwitcher textswitcher.setInAnimation(in); textswitcher.setOutAnimation(out); } // Click listener method for button void setListener() { // ClickListener for NEXT button // When clicked on Button TextSwitcher will switch between texts // The current Text will go OUT and next text will come in with // specified animation next.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { currentIndex++; // If index reaches maximum reset it if (currentIndex == messageCount) currentIndex = 0; // Set the textSwitcher text according to current index from // string array textswitcher.setText(textToShow[currentIndex]); } }); } // Set Factory for the textSwitcher *Compulsory part void setFactory() { textswitcher.setFactory(new ViewFactory() { public View makeView() { // Create run time textView with some attributes like gravity, // color, etc. TextView myText = new TextView(MainActivity.this); myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); myText.setTextSize(30); myText.setTextColor(Color.RED); return myText; } }); } } |
4. Finally, run the app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.