Hello Readers! In this post, we are going to learn about how to use android textSwitcher widget in any android application. We will also go through different attributes of textSwitcher widget that can be used to customise it.
Output
Getting Started
TextSwitcher widget can be defined as below –
TextSwitcher, subclass of ViewSwitcher, is a widget that contains only children of type TextView. It is used to switch between views using animations. It animates the current text out and animates the next text in.
Attributes of Android TextSwitcher Widget
Some of the popular attributes of android textSwitcher widget inherited from viewAnimator are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animateFirstView | Defines whether to animate the current view when the view animation is first displayed |
2 | android:inAnimation | Specifies the animation to use when view is shown |
3 | android:outAnimation | Specifies the animation to use when view is hidden |
Some of the popular attributes of android textSwitcher inherited from FrameLayout are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:foregroundGravity | Specifies gravity to apply to the foreground drawable |
2 | android:measureAllChildren | Specifies whether to measure all children or just those in VISIBLE or INVISIBLE state while measuring |
Some of the popular attributes of android textSwitcher inherited from ViewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animateLayoutChanges | Defines whether layoutTransition should run when there is any changes in layout |
2 | android:animationCache | Defines whether layout animations should create drawing cache for their children |
3 | android:clipToPadding | Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero. |
4 | android:layoutAnimation | Defines layout animation to use when the viewGroup is laid out for the first time |
5 | android:layoutMode | Defines the layout mode of this viewGroup |
Some of the popular attributes of android textSwitcher inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Defines alpha of the view |
2 | android:clickable | Defines whether this view is clickable |
3 | android:background | Defines drawable for the background of the view |
4 | android:elevation | Defines z-depth of the view |
5 | android:id | Defines unique id of the view |
6 | android:onClick | Defines what to do when this view is clicked |
7 | android:padding | Defines padding of the view |
8 | android:visibility | Defines visibility(VISIBLE, INVISIBLE or GONE) of the view |
Example of Android TextSwitcher Widget
At first, we will create android application. Then, we will use textSwitcher widget in this application.
1. Creating New Project
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as TextSwitcher. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use textSwitcher widget in the application.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">TextSwitcher</string> <string name="next">Next</string> </resources>
3. Use TextSwitcher Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="40dp" android:text="@string/next"/> <TextSwitcher android:id="@+id/textSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"/> </LinearLayout>
In activity_main.xml file, we have defined button and textSwitcher widgets. Now, we will access these widgets in java file to perform some operations on them.
4. Access TextSwitcher Widget in java file
Open src/main/java/com.tutorialwing.textswitcher/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.textswitcher; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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 MainActivity extends AppCompatActivity { final String[] textList = {"Panda", "Tiger", "Zebra", "Lion", "Deer", "Goat", "Ape", "Monkey", "Human"}; int index = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextSwitcher textSwitcher = findViewById(R.id.textSwitcher); if (textSwitcher != null) { textSwitcher.setFactory(new ViewSwitcher.ViewFactory() { public View makeView() { TextView textView = new TextView(MainActivity.this); textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); textView.setTextSize(32); textView.setTextColor(Color.RED); return textView; } }); textSwitcher.setText(textList[index]); Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); textSwitcher.setInAnimation(in); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); textSwitcher.setOutAnimation(out); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { index = ((index + 1) < textList.length) ? (index + 1) : 0; textSwitcher.setText(textList[index]); } }); } } }
We have accessed textSwitcher widget in the java file. Then, we have set factory that defines the view between which textSwitcher will switch. Then, we have set in and out animations that will be used when textView appears/disappears on/from the screen. At last, we have accessed button and set click listener that displays the next textViews when it is clicked.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.textswitcher" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When we run the program, we will get output as shown above.
That’s end of tutorial on Android TextSwitcher widget.
You must be logged in to post a comment.