Hi all…..

Today I will show you how to do a simple and beautiful text animation while switching the text in a textView in android

The advantage is that you don’t need a seperate xml file for animation in this program, all is done inside the java code.

TextSwitcher

TextSwitcher

This is the main java file.

package pack.coderzheaven;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
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;

public class TextSwitcherDemo extends Activity implements ViewSwitcher.ViewFactory,
        View.OnClickListener {

    private TextSwitcher mSwitcher;

    private int mCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);

        Animation in = AnimationUtils.loadAnimation(this,  android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);

        Button nextButton = (Button) findViewById(R.id.next);
        nextButton.setOnClickListener(this);

        updateCounter();
    }

    public void onClick(View v) {
        mCounter++;
        updateCounter();
    }

    private void updateCounter() {
        mSwitcher.setText(String.valueOf(mCounter));
    }

    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
        t.setTextSize(70);
        t.setTextColor(Color.RED);
        return t;
    }
}

Here we use a widget called TextSwitcher in android.
Now the main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

	<TextView android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textStyle="bold|italic"/>

    <Button android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch"
        android:textStyle="bold|italic" />

    <TextSwitcher android:id="@+id/switcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">TextSwitcher Demo from CoderzHeaven</string>
    <string name="app_name">TextSwitcher Demo</string>
</resources>

AndroidManifest.xml file contents.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TextSwitcherDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

If you like this post then click on the +1 button on top of the post and leave your valuable comments also.

 

3 Comments

  1. Umair January 18, 2013 at 6:02 am

    Nice article! Proved really helpful.
    Thanks for sharing

     
  2. Pingback: Changing Text color animation in Android : Android Community - For Application Development

  3. Pingback: How to create animation by default in a GrdiView in Android? | All Things Gadget

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.