The Switcher specialize ViewSwitcher .That implement only for children of type textView . A TextSwitcher useful animate labale on Screen. show example.
MainActivity.java
..............................................................................................................................................................
package com.androidbeginner.testcode;
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.ViewFactory;
public class MainActivity extends Activity {
private TextSwitcher TxtSwitcher;
Button Nextbtn;
// Array of String to Show In TextSwitcher
String textToShow[]={"A","B","C","D","E","F","G","H","I","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
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);
// get The references
Nextbtn=(Button)findViewById(R.id.butn_signUp);
TxtSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1);
// Set the ViewFactory of the TextSwitcher that will create TextView object when asked
TxtSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
return myText;
}
});
// 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
TxtSwitcher.setInAnimation(in);
TxtSwitcher.setOutAnimation(out);
// 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
Nextbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==messageCount)
currentIndex=0;
TxtSwitcher.setText(textToShow[currentIndex]);
}
});
}
}
acivity_main.xml
..............................................................................................................................................................
<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"
android:id="@+id/text"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.androidbeginner.testcode.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="Androidbeginner"
android:textSize="20sp" />
<Button
android:id="@+id/butn_signUp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:padding="10dp"
android:background="@drawable/button_action"
android:text="Next" />
<TextSwitcher
android:id="@+id/textSwitcher1"
android:layout_width="400dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:textSize="80sp"
android:layout_marginTop="100dp" >
</TextSwitcher>
</RelativeLayout>
0 comments