|
1 |
| -# AndroidAutoTextSwitcher |
2 |
| -[](https://jitpack.io/#masoudsaraf/androidautotextswitcher) |
| 1 | +# AutoTextSwitcher |
| 2 | +[](https://jitpack.io/#masoudsaraf/androidautotextswitcher) [](https://android-arsenal.com/api?level=21) |
3 | 3 |
|
4 |
| -I do this later too. |
| 4 | +This is an Android project allowing to auto change text in specific period of time with your chosen animation. |
| 5 | + |
| 6 | +## Add dependency |
| 7 | + |
| 8 | +- First add repository in your `build.gradle` file at project level: |
| 9 | + |
| 10 | +```groovy |
| 11 | +allprojects { |
| 12 | + repositories { |
| 13 | + .... |
| 14 | + maven { url 'https://jitpack.io' } |
| 15 | + } |
| 16 | +} |
| 17 | +``` |
| 18 | +Or with newest Gradle version (+7.2.0) do like this in `settings.gradle` file: |
| 19 | +```groovy |
| 20 | +pluginManagement { |
| 21 | + repositories { |
| 22 | + .... |
| 23 | + } |
| 24 | +} |
| 25 | +dependencyResolutionManagement { |
| 26 | + .... |
| 27 | + repositories { |
| 28 | + .... |
| 29 | + maven { url 'https://jitpack.io' } |
| 30 | + } |
| 31 | +} |
| 32 | +.... |
| 33 | +``` |
| 34 | +- And then add dependency to your project in the `build.gradle` file at app level: |
| 35 | +```groovy |
| 36 | +implementation 'com.github.masoudsaraf:androidautotextswitcher:1.0.1' |
| 37 | +``` |
| 38 | + |
| 39 | +## How to use in XML |
| 40 | +You can set textArray from XML or java code. In XML easiest way is add your text in strings resource and create a text array like this: |
| 41 | +```xml |
| 42 | +<resources> |
| 43 | + .... |
| 44 | + <string name="first_text">Show awesome first text</string> |
| 45 | + <string name="sec## Headingond_text">Show awesome second text</string> |
| 46 | + |
| 47 | + <string-array name="textArray"> |
| 48 | + <item>@string/first_text</item> |
| 49 | + <item>@string/second_text</item> |
| 50 | + </string-array> |
| 51 | +</resources> |
| 52 | +``` |
| 53 | +And then use created textArray as follow: |
| 54 | +```xml |
| 55 | +<com.masoud.autotextswitcher.AutoTextSwitcher |
| 56 | + android:id="@+id/textSwitcher" |
| 57 | + android:layout_width="match_parent" |
| 58 | + android:layout_height="wrap_content" |
| 59 | + android:inAnimation="@anim/text_in_animation" |
| 60 | + android:outAnimation="@anim/text_out_animation" |
| 61 | + app:textArray="@array/textArray" |
| 62 | + app:changeAnimationTime="3000" |
| 63 | + app:autoStart="true"/> |
| 64 | +``` |
| 65 | +You can use following properties:<br> |
| 66 | +`android:inAnimation` Set animation for show next text. <br> |
| 67 | +`android:outAnimation` Set animation for hide current text.<br> |
| 68 | +`app:textArray` Set array of string.<br> |
| 69 | +`app:changeAnimationTime` Set time in milliseconds.<br> |
| 70 | +`app:autoStart` Indicates auto start changing text or not |
| 71 | + |
| 72 | +## Complete work in java code |
| 73 | +In your Activity class access to `AutoTextSwitcher` element with `findViewById` and then set factory to create a text view with your preferred properties: |
| 74 | +```java |
| 75 | +public class MainActivity extends AppCompatActivity |
| 76 | +{ |
| 77 | + protected void onCreate(Bundle savedInstanceState) |
| 78 | + { |
| 79 | + ... |
| 80 | + AutoTextSwitcher textSwitcher = findViewById(R.id.textSwitcher); |
| 81 | + textSwitcher.setFactory(() -> |
| 82 | + { |
| 83 | + AppCompatTextView textView = new AppCompatTextView(MainActivity.this); |
| 84 | + // set custom text appearance |
| 85 | + textView.setTextAppearance(MainActivity.this, R.style.InfoTextAppearanceStyle); |
| 86 | + textView.setPadding(10, 10, 10, 10); |
| 87 | + textView.setShadowLayer(12, 0, -5.5f, Color.parseColor("#65FFFFFF")); |
| 88 | + return textView; |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | +## Public methods |
| 94 | +Call `startTextAnimation()` for manually start changing texts with animation.<br> |
| 95 | +Call `stopTextAnimation()` for stop animation and changing texts.<br> |
| 96 | +Call `isTextAnimationIsRunning()` to determine is animation and changing text is activate or not.<br> |
| 97 | +Call `setTextArray(List<CharSequence>)` to set new text array from code. even you can have Spannable text in array.<br> |
| 98 | +Call `appendToTextArray(CharSequence newText)` to append a text to exists array. Just like above you can use Spannable text. |
0 commit comments