Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make TextSwitcher in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 310
    Comment on it

    TextSwitcher is useful to animate a label on screen. TextSwitcher animates the current text out and animates the new text. In below example Frist I have created a main layout, In main layout I have added TextSwitcher attributes. See the below example code it will clearly described you how to create TextSwitcher.

    Step(1)-MainActivity-

    public class MainActivity extends AppCompatActivity {
    
        Button buttonNext;
    TextSwitcher textSwitcher;
    Animation slide_in_left,slide_out_right;
    
    String[]TextToSwitched={"one","two","three","four","five"};
    int curIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonNext =(Button)findViewById(R.id.next);
        textSwitcher=(TextSwitcher)findViewById(R.id.textswithcer);
        slide_in_left= AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
        slide_out_right=AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
    
        textSwitcher.setInAnimation(slide_in_left);
        textSwitcher.setOutAnimation(slide_out_right);
        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(MainActivity.this);
                textView.setTextSize(30);
                textView.setTextColor(Color.RED);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                textView.setTypeface(Typeface.DEFAULT_BOLD);
                textView.setShadowLayer(10, 10, 10, Color.BLUE);
                return textView;
            }
        });
    
    
        curIndex=0;
        textSwitcher.setText(TextToSwitched[curIndex]);
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (curIndex == TextToSwitched.length-1){
                    curIndex=0;
                    textSwitcher.setText(TextToSwitched[curIndex]);
                }else {
                    textSwitcher.setText(TextToSwitched[++curIndex]);
                }
    
            }
        });
    
    
    }
    }
    

    Step(2)-main.xml-

    <LinearLayout 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:orientation="vertical"
        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=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="web"
            android:text="Microsoft"
            android:textStyle="bold"
            android:background="#abf299"
            android:textSize="25dp" />
        <TextSwitcher
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textswithcer"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/next"/>
    
    
    
    </LinearLayout>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: