How to create Rotating Text in android | Android Studio | Java
In this tutorial, we’re going to create a beautiful android app layout using rotating texts. Rotating text is an Android library that can be used to make text switching painless and beautiful, with the use of interpolators, typefaces, and more customizations.
Part 1: Add Dependency
Add the following dependency in the app’s build.gradle
dependencies {
implementation ‘com.sdsmdg.harjot:rotatingtext:1.0.2’
}
Part 2: Example 1 (Simple rotating effect)
In the main XML file creates a Rotating text wrapper.
<com.sdsmdg.harjot.rotatingtext.RotatingTextWrapper
android:id=”@+id/custom_switcher”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
- In the main java file defines the two main parts of the Rotating Text i.e. RotatingTextWrapper and Rotatable.
- In RotatingTextWrapper defines the actual layout of the text and the positions of the rotating text.
- And in the Rotatable, we define the rotating text properties, such as color, size, typeface, interpolator, update duration, animation duration, and center align. Below we will discuss these properties.
Now in the main java file
RotatingTextWrapper rotatingTextWrapper = (RotatingTextWrapper) findViewById(R.id.custom_switcher);
rotatingTextWrapper.setSize(35);
Rotatable rotatable = new Rotatable(Color.parseColor(“#FFA036”), 1000, “Word”, “Word01”, “Word02”);
rotatable.setSize(35);
rotatable.setAnimationDuration(500);
rotatingTextWrapper.setContent(“This is ?”, rotatable);
// here “?” denotes the position of the rotatable
Preview
Part 2: Example 2 (Typeface + Interpolator)
In the main XML file
<com.sdsmdg.harjot.rotatingtext.RotatingTextWrapper
android:id=”@+id/custom_switcher”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
In the main java file
Typeface typeface = Typeface.createFromAsset(getAssets(), “fonts/Raleway-Light.ttf”);
Typeface typeface2 = Typeface.createFromAsset(getAssets(), “fonts/Reckoner_Bold.ttf”);
RotatingTextWrapper rotatingTextWrapper = (RotatingTextWrapper) findViewById(R.id.custom_switcher);
rotatingTextWrapper.setSize(35);
rotatingTextWrapper.setTypeface(typeface2);
Rotatable rotatable = new Rotatable(Color.parseColor(“#FFA036”), 1000, “Word”, “Word01”, “Word02”);
rotatable.setSize(35);
rotatable.setAnimationDuration(500);
rotatable.setTypeface(typeface);
rotatable.setInterpolator(new BounceInterpolator());
rotatingTextWrapper.setContent(“This is ?”, rotatable);
Preview
Part 3: Documentation
RotatingTextWrapper
- Content (setContent(..)): set the actual content. Compose of a String and array of Rotatable.
- Typeface (setTypeface(..)): Set the typeface of the non-ratating text
- Size (setSize(..)): set the size of the non-rotating text.
- Pause (pause(x)): Method to pause the ‘x’th rotatable.
- Resume (resume(x)): Method to resume the ‘x’th rotatable.
Rotatable
- Color (setColor(..)): set the color of the rotating text associated with this rotatable.
- Size (setSize(..)): set the size of the rotating text associated with this rotatable
- Typeface (setTypeface(…)): Set the typeface of the rotating text associated with this rotatable
- Interpolator (setInterpolator(..)): Set the animation interpolator used while switching text
- Update Duration (setUpdateDuration(..)): Set the interval between switching the words
- Animation Duration (setAnimationDuration(..)): Set the duration of the switching animation
- Center Align (setCenter(..)): Align the rotating text to the center of the textview if set to true