Skip to content

Commit 01f4c44

Browse files
committed
Added an example app, added setTexts(String[]), added setTimeout(int), removed default textAppearance and textColor, resume and pause shuold now be called automatically, fixed bugs
1 parent b327910 commit 01f4c44

File tree

10 files changed

+153
-8
lines changed

10 files changed

+153
-8
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies {
2424
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2525
exclude group: 'com.android.support', module: 'support-annotations'
2626
})
27+
compile project(':fadingtextview')
2728
compile 'com.android.support:appcompat-v7:25.1.0'
29+
compile 'com.android.support:design:25.1.0'
2830
testCompile 'junit:junit:4.12'
2931
}

app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
23
package="com.tomerrosenfeld.fadingtextview">
34

@@ -7,7 +8,16 @@
78
android:label="@string/app_name"
89
android:supportsRtl="true"
910
android:theme="@style/AppTheme">
10-
11+
<activity
12+
android:name=".MainActivity"
13+
android:label="@string/title_activity_main"
14+
android:theme="@style/AppTheme.NoActionBar">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
<action android:name="android.intent.action.VIEW" />
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
1121
</application>
1222

1323
</manifest>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:fitsSystemWindows="true"
8+
tools:context="com.tomerrosenfeld.fadingtextview.MainActivity">
9+
10+
<android.support.design.widget.AppBarLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:theme="@style/AppTheme.AppBarOverlay">
14+
15+
<android.support.v7.widget.Toolbar
16+
android:id="@+id/toolbar"
17+
android:layout_width="match_parent"
18+
android:layout_height="?attr/actionBarSize"
19+
android:background="?attr/colorPrimary"
20+
app:popupTheme="@style/AppTheme.PopupOverlay" />
21+
22+
</android.support.design.widget.AppBarLayout>
23+
24+
<include layout="@layout/content_main" />
25+
26+
<android.support.design.widget.FloatingActionButton
27+
android:id="@+id/fab"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_gravity="bottom|end"
31+
android:layout_margin="@dimen/fab_margin"
32+
app:srcCompat="@android:drawable/ic_dialog_email" />
33+
34+
</android.support.design.widget.CoordinatorLayout>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/content_main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:paddingBottom="@dimen/activity_vertical_margin"
9+
android:paddingLeft="@dimen/activity_horizontal_margin"
10+
android:paddingRight="@dimen/activity_horizontal_margin"
11+
android:paddingTop="@dimen/activity_vertical_margin"
12+
app:layout_behavior="@string/appbar_scrolling_view_behavior"
13+
tools:context="com.tomerrosenfeld.fadingtextview.MainActivity"
14+
tools:showIn="@layout/activity_main">
15+
16+
<com.tomer.fadingtextview.FadingTextView
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:height="64dp"
20+
android:gravity="center"
21+
android:id="@+id/fadingTextView"
22+
app:texts="@array/examples"
23+
app:timeout="500" />
24+
25+
</RelativeLayout>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<resources>
2+
3+
<style name="AppTheme.NoActionBar">
4+
<item name="windowActionBar">false</item>
5+
<item name="windowNoTitle">true</item>
6+
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
7+
<item name="android:statusBarColor">@android:color/transparent</item>
8+
</style>
9+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>

app/src/main/res/values/dimens.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
<dimen name="fab_margin">16dp</dimen>
6+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<resources>
22
<string name="app_name">FadingTextView</string>
3+
<string name="title_activity_main">MainActivity</string>
4+
<string-array name="examples">
5+
<item>Text number 1</item>
6+
<item>This is a different text</item>
7+
<item>WOW many texts!</item>
8+
</string-array>
39
</resources>

app/src/main/res/values/styles.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@
88
<item name="colorAccent">@color/colorAccent</item>
99
</style>
1010

11+
<style name="AppTheme.NoActionBar">
12+
<item name="windowActionBar">false</item>
13+
<item name="windowNoTitle">true</item>
14+
</style>
15+
16+
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
17+
18+
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
19+
1120
</resources>

fadingtextview/src/main/java/com/tomer/fadingtextview/FadingTextView.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import android.os.Build;
66
import android.os.Handler;
77
import android.support.annotation.ArrayRes;
8+
import android.support.annotation.NonNull;
89
import android.support.annotation.RequiresApi;
9-
import android.support.v4.content.ContextCompat;
1010
import android.util.AttributeSet;
1111
import android.view.animation.Animation;
1212
import android.view.animation.AnimationUtils;
@@ -55,7 +55,19 @@ public void resume() {
5555

5656
public void pause() {
5757
isShown = false;
58-
handler.removeCallbacksAndMessages(null);
58+
stopAnimation();
59+
}
60+
61+
@Override
62+
protected void onDetachedFromWindow() {
63+
super.onDetachedFromWindow();
64+
pause();
65+
}
66+
67+
@Override
68+
protected void onAttachedToWindow() {
69+
super.onAttachedToWindow();
70+
resume();
5971
}
6072

6173
private void init(Context context) {
@@ -64,10 +76,6 @@ private void init(Context context) {
6476
handler = new Handler();
6577
isShown = true;
6678
setMaxLines(1);
67-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
68-
setTextAppearance(android.R.style.TextAppearance_Material_Button);
69-
setTextColor(ContextCompat.getColor(context, android.R.color.white));
70-
}
7179
}
7280

7381
private void handleAttrs(Context context, AttributeSet attrs) {
@@ -83,7 +91,37 @@ public CharSequence[] getTexts() {
8391
}
8492

8593
public void setTexts(@ArrayRes int texts) {
86-
this.texts = getResources().getStringArray(texts);
94+
if (getResources().getStringArray(texts).length < 1)
95+
throw new IllegalArgumentException("There must be at least one text");
96+
else {
97+
this.texts = getResources().getStringArray(texts);
98+
stopAnimation();
99+
position = 0;
100+
startAnimation();
101+
}
102+
}
103+
104+
public void setTexts(@NonNull String[] texts) {
105+
if (texts.length < 1)
106+
throw new IllegalArgumentException("There must be at least one text");
107+
else {
108+
this.texts = texts;
109+
stopAnimation();
110+
position = 0;
111+
startAnimation();
112+
}
113+
}
114+
115+
public void setTimeout(int timeout) {
116+
if (timeout < 1)
117+
throw new IllegalArgumentException("Timeout must be longer than 0");
118+
else
119+
this.timeout = timeout;
120+
}
121+
122+
private void stopAnimation() {
123+
handler.removeCallbacksAndMessages(null);
124+
if (getAnimation() != null) getAnimation().cancel();
87125
}
88126

89127
protected void startAnimation() {

0 commit comments

Comments
 (0)