Android TextSwitcher With Animation Example Tutorial

How to create android app with animated transition text using string array one by one on button click.

In this tutorial we are going to use TextSwitcher widget which is used to show string array elements with animation effect one by one using move button. It is mostly used to when android developer needs to show some string words with swipe left to right animation. So here is the complete step by step tutorial for Android TextSwitcher With Animation Example Tutorial.

android-project-download-code-button

Live Demo :

Android TextSwitcher With Animation Example Tutorial.

Code for MainActivity.java file.

package com.android_examples.textswitcher_android_examplescom;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

    TextSwitcher textSwitcher ;
    Button Next ;
    int StringIndex = 0 ;
    String[] Row = {
            "ONE",
            "TWO",
            "THREE",
            "FOUR",
            "FIVE",
            "SIX",
            "SEVEN"
    };

    TextView textView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textSwitcher = (TextSwitcher)findViewById(R.id.textswitcher1);
        Next = (Button)findViewById(R.id.button);

        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(StringIndex == Row.length-1){

                    StringIndex = 0;

                    textSwitcher.setText(Row[StringIndex]);
                }
                else{

                    textSwitcher.setText(Row[++StringIndex]);
                }
            }
        });

        textSwitcher.setFactory(new ViewSwitcher.ViewFactory(){

            @Override
            public View makeView() {

                textView = new TextView(MainActivity.this);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(40);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                return textView;

            }});

        textSwitcher.setText(Row[StringIndex]);

    }
}

Code for activity_main.xml layout file.

<?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"
    tools:context="com.android_examples.textswitcher_android_examplescom.MainActivity"
    android:background="#FFF8E1">

    <TextSwitcher
        android:id="@+id/textswitcher1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right"
        android:layout_marginTop="65dp"
        />

    <Button
        android:text="Move to Next Text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />

</RelativeLayout>

Screenshots:

textswitcher-1

 

Android TextSwitcher With Animation Example Tutorial

Click here to download Android TextSwitcher With Animation Example Tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *