Create A TextSwitcher App In Android Application Using Android Studio

Introduction

In this article, I will explain how to create TextSwitcher app in Android applications using Android Studio.

Requirements

  • Android Studio 2.1.3

If you want to create a TextSwitcher app, you should follow the steps given below.

Step 1

Now, open Android Studio and you can choose the file, and subsequently choose New. Afterwords, select New Project.

Android

Step 2

Here, you can create your application name and choose where your project is stored on the location. Then, click NEXT button.

Android

Now, we can select the version of Android; it is Target Android Devices.

Android

Step 3

Here, we can add the activity and click Next button.

Android

Now, we can write the activity name and click Finish button.

Android

Step 4

Now, open your project and you will go to activity_main.xml and afterwards, build the design. You should choose toolbox if you want some options (TextSwitcher, button), and use the drag and drop method.

Android

Now, we can see the Graphical User Interface design.

Android

Step 5

Here, you need to build on the design and write the .XML code.

activity_mai.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <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: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="xyz.rvconstructions.www.textswitcherapp.MainActivity">  
  3.     <TextSwitcher android:id="@+id/simpleTextSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="50dp" />  
  4.     <Button android:id="@+id/buttonNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:layout_gravity="center" android:background="#4b5500" android:textColor="#fff" android:textStyle="bold" android:text="NEXT" />  
  5. </RelativeLayout>  
Step 6

Now, you will go to the MainActivity.java page and build Java code.

First of all, you will declare a file that's an extension file.

Android

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.textswitcherapp;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.Gravity;  
  5. import android.view.View;  
  6. import android.view.animation.Animation;  
  7. .import android.view.animation.AnimationUtils;  
  8. .import android.widget.Button;  
  9. import android.widget.TextSwitcher;  
  10. import android.widget.TextView;  
  11. import android.widget.ViewSwitcher;  
  12. public class MainActivity extends AppCompatActivity {  
  13.     private TextSwitcher firstTextSwitcher;  
  14.     Button bttnNext;  
  15.     String strings[] = {  
  16.         "First Text Switcher ",  
  17.         " SecondText Switcher ",  
  18.         "Third Text Switcher ",  
  19.         " Fourth Text Switcher ",  
  20.         "Five Text Switcher "  
  21.     };  
  22.     int messageCount = strings.length;  
  23.     int currentIndex = -1;  
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         bttnNext = (Button) findViewById(R.id.buttonNext);  
  29.         firstTextSwitcher = (TextSwitcher) findViewById(R.id.simpleTextSwitcher);  
  30.         firstTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {  
  31.             public View makeView() {  
  32.                 TextView t = new TextView(MainActivity.this);  
  33.                 t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);  
  34.                 t.setTextSize(36);  
  35.                 return t;  
  36.             }  
  37.         });  
  38.         Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);  
  39.         Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);  
  40.         firstTextSwitcher.setInAnimation( in );  
  41.         firstTextSwitcher.setOutAnimation(out);  
  42.         firstTextSwitcher.setCurrentText("click to next button to switch text");  
  43.         bttnNext.setOnClickListener(new View.OnClickListener() {  
  44.             public void onClick(View v) {  
  45.                 currentIndex++;  
  46.                 if (currentIndex == messageCount)  
  47.                     urrentIndex = 0;  
  48.                 firstTextSwitcher.setText(strings[currentIndex]);  
  49.             }  
  50.         });  
  51.     }  
  52. }  
Step 7

Here, you will go to run it and select Run-> Run app option.

Android

Here, you will choose Emulator or the devices; it is Nokia Nokia _X.

Android

Step 8

Here, you can see the output.



Now, you will click on Next button to switch the text.

Android

Android

Android

Next Recommended Readings