Android textSwitcher Example Oreo Latest Source Code

Android textSwitcher example switch text each time button is clicked

Android textSwitcher used to switch text purpose change text each time when button is clicked. Here is the great android textSwitcher example serve changing text android inbuilt class feature to change text automatically or by user button press. Get and watch latest video tutorial android textSwitcher.

package attraction.blessme.begin;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 MainActivity extends AppCompatActivity {

    TextSwitcher textSwitcher;
    Button tapToNext;

    String textInSwitcher[] = {"TextSwitcher text 1", "Next TextSwitcher 2 working", "Android TextSwitcher 3", "TextSwitcher ATTRACTION 4", "TextSwitcher to change text"};

    int switchText = textInSwitcher.length;
    int counter = -1;

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

        textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
        tapToNext = (Button) findViewById(R.id.nextBtn);

        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView switchView = new TextView(getApplicationContext());
                switchView.setTextSize(30);
                switchView.setTextColor(Color.BLUE);
                switchView.setText("Click Button To Get Next Text");
                return switchView;
            }
        });

        //animation, how next text will be appear.
        Animation animationOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        Animation animationIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

        //No need to add additional anim xml file, above those are default.
        textSwitcher.setOutAnimation(animationOut);
        textSwitcher.setInAnimation(animationIn);
    }

    public void nextButton(View view) {
        counter++;
        if (counter == switchText)
            counter = 0;
        textSwitcher.setText(textInSwitcher[counter]);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#98e6f1"
    >
    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="184dp"
        android:background="#82ffd5"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/nextBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="90dp"
        android:onClick="nextButton"
        android:background="#ff5865"
        android:text="CLICK FOR NEXT TEXT"
        android:layout_above="@+id/textView"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:textSize="25sp"
        android:gravity="bottom|center"
        android:text="www.NextGenEarn.com"
        android:linksClickable="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="26dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#32c4f5"
        android:textSize="35sp"
        android:text="TEXTSWITCHER"
        android:layout_above="@+id/nextBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="116dp" />

</RelativeLayout>

Get more latest video tutorials from ANDROID ATTRACTION supported android newer OS version Oreo Marshmallow Nougat etc.

Add a Comment

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