Skip to content

Commit 830e9c1

Browse files
committed
Fixed striped process button,
Fixed attrs
1 parent 5189b98 commit 830e9c1

11 files changed

Lines changed: 202 additions & 98 deletions

File tree

app/src/androidTest/java/com/github/nikartm/strippedprogressbutton/ExampleInstrumentedTest.java renamed to app/src/androidTest/java/com/github/nikartm/strippedprocessbutton/ExampleInstrumentedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.nikartm.strippedprogressbutton;
1+
package com.github.nikartm.strippedprocessbutton;
22

33
import android.content.Context;
44
import android.support.test.InstrumentationRegistry;

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.github.nikartm.strippedprogressbutton">
3+
package="com.github.nikartm.strippedprocessbutton">
44

55
<application
66
android:allowBackup="true"

app/src/main/java/com/github/nikartm/strippedprogressbutton/MainActivity.java renamed to app/src/main/java/com/github/nikartm/strippedprocessbutton/MainActivity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
package com.github.nikartm.strippedprogressbutton;
1+
package com.github.nikartm.strippedprocessbutton;
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
55
import android.widget.Button;
66

7-
import com.github.nikartm.support.StripedProgressButton;
7+
import com.github.nikartm.support.StripedProcessButton;
88

99

1010
public class MainActivity extends AppCompatActivity {
1111

12-
private StripedProgressButton btnStart;
12+
private StripedProcessButton stripedButton;
1313
private Button btnStop;
1414

1515
@Override
1616
protected void onCreate(Bundle savedInstanceState) {
1717
super.onCreate(savedInstanceState);
1818
setContentView(R.layout.activity_main);
19-
btnStart = findViewById(R.id.btn_start);
19+
stripedButton = findViewById(R.id.btn_start);
2020
btnStop = findViewById(R.id.btn_stop);
2121

22-
btnStart.setOnClickListener(v -> btnStart.start());
23-
btnStop.setOnClickListener(v -> btnStart.stop());
22+
stripedButton.setOnClickListener(v -> stripedButton.start());
23+
btnStop.setOnClickListener(v -> stripedButton.stop());
2424
}
2525
}

app/src/main/res/layout/activity_main.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
android:text="Example of using progress button"
1717
android:textSize="16sp" />
1818

19-
<com.github.nikartm.support.StripedProgressButton
19+
<com.github.nikartm.support.StripedProcessButton
2020
android:id="@+id/btn_start"
2121
android:layout_width="match_parent"
2222
android:layout_height="44dp"
2323
android:layout_marginBottom="8dp"
2424
android:layout_marginEnd="16dp"
2525
android:layout_marginStart="16dp"
2626
android:layout_marginTop="16dp"
27-
android:text="Example"
27+
android:text="Start process"
28+
app:spb_loadingText="Loading..."
2829
android:clickable="true"
2930
android:focusable="true"
3031
android:textColor="#ffffff"
@@ -36,6 +37,11 @@
3637
android:layout_height="wrap_content"
3738
android:layout_marginStart="16dp"
3839
android:layout_marginTop="16dp"
40+
android:textSize="16dp"
41+
android:textAllCaps="false"
42+
android:textColor="@android:color/white"
43+
android:background="@drawable/btn_rounded"
44+
android:foreground="@drawable/ripple"
3945
android:text="Stop"/>
4046

4147
</android.support.v7.widget.LinearLayoutCompat>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">StrippedProgressButton-Android</string>
2+
<string name="app_name">StrippedProcessButton-Android</string>
33
</resources>

app/src/test/java/com/github/nikartm/strippedprogressbutton/ExampleUnitTest.java renamed to app/src/test/java/com/github/nikartm/strippedprocessbutton/ExampleUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.nikartm.strippedprogressbutton;
1+
package com.github.nikartm.strippedprocessbutton;
22

33
import org.junit.Test;
44

support/src/main/java/com/github/nikartm/support/AttributeController.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,35 @@
77
import com.github.nikartm.support.constant.Constants;
88

99
/**
10+
* Controller attrs of striped process button
1011
* @author Ivan V on 29.03.2018.
1112
* @version 1.0
1213
*/
13-
public class AttributeController {
14+
class AttributeController {
1415

1516
private AnimatedStripedDrawable drawable;
17+
private String loadingText;
1618

1719
public AttributeController(Context context, AttributeSet attrs) {
1820
drawable = new AnimatedStripedDrawable(context);
1921
initAttrs(context, attrs);
2022
}
2123

2224
private void initAttrs(Context context, AttributeSet attrs) {
23-
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StripedProgressButton);
25+
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StripedProcessButton);
2426
try {
25-
float stripeWidth = typedArray.getDimension(R.styleable.StripedProgressButton_spb_stripeWidth, Constants.STRIPE_WIDTH);
26-
float stripeAlpha = typedArray.getFloat(R.styleable.StripedProgressButton_spb_stripeAlpha, Constants.STRIPE_ALPHA);
27-
int stripeTilt = typedArray.getInt(R.styleable.StripedProgressButton_spb_stripeTilt, Constants.STRIPE_TILT);
28-
int stripeDuration = typedArray.getInt(R.styleable.StripedProgressButton_spb_stripeDuration, Constants.DURATION);
29-
int background = typedArray.getColor(R.styleable.StripedProgressButton_spb_background, Constants.DEF_BACKGROUND);
30-
int mainStripeColor = typedArray.getColor(R.styleable.StripedProgressButton_spb_mainStripColor, Constants.DEF_MAIN_STRIPE);
31-
int subStripeColor = typedArray.getColor(R.styleable.StripedProgressButton_spb_subStripeColor, Constants.DEF_SUB_STRIPE);
32-
float cornerRadius = typedArray.getFloat(R.styleable.StripedProgressButton_spb_cornerRadius, Constants.CORNER);
33-
boolean stripeRevert = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_stripeRevert, Constants.REVERT);
34-
boolean showStripes = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_showStripes, Constants.SHOW_STRIPES);
35-
boolean stripeGradient = typedArray.getBoolean(R.styleable.StripedProgressButton_spb_stripeGradient, Constants.GRADIENT);
27+
float stripeWidth = typedArray.getDimension(R.styleable.StripedProcessButton_spb_stripeWidth, Constants.STRIPE_WIDTH);
28+
float stripeAlpha = typedArray.getFloat(R.styleable.StripedProcessButton_spb_stripeAlpha, Constants.STRIPE_ALPHA);
29+
int stripeTilt = typedArray.getInt(R.styleable.StripedProcessButton_spb_stripeTilt, Constants.STRIPE_TILT);
30+
int stripeDuration = typedArray.getInt(R.styleable.StripedProcessButton_spb_stripeDuration, Constants.DURATION);
31+
int background = typedArray.getColor(R.styleable.StripedProcessButton_spb_background, Constants.DEF_BACKGROUND);
32+
int mainStripeColor = typedArray.getColor(R.styleable.StripedProcessButton_spb_mainStripColor, Constants.DEF_MAIN_STRIPE);
33+
int subStripeColor = typedArray.getColor(R.styleable.StripedProcessButton_spb_subStripeColor, Constants.DEF_SUB_STRIPE);
34+
float cornerRadius = typedArray.getFloat(R.styleable.StripedProcessButton_spb_cornerRadius, Constants.CORNER);
35+
boolean stripeRevert = typedArray.getBoolean(R.styleable.StripedProcessButton_spb_stripeRevert, Constants.REVERT);
36+
boolean showStripes = typedArray.getBoolean(R.styleable.StripedProcessButton_spb_showStripes, Constants.SHOW_STRIPES);
37+
boolean stripeGradient = typedArray.getBoolean(R.styleable.StripedProcessButton_spb_stripeGradient, Constants.GRADIENT);
38+
loadingText = typedArray.getString(R.styleable.StripedProcessButton_spb_loadingText);
3639

3740
drawable.setStripeWidth(stripeWidth)
3841
.setStripeAlpha(stripeAlpha)
@@ -54,4 +57,8 @@ public AnimatedStripedDrawable getStripedDrawable() {
5457
return drawable;
5558
}
5659

60+
public String getLoadingText() {
61+
return loadingText;
62+
}
63+
5764
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.github.nikartm.support;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.drawable.Animatable;
6+
import android.support.v7.widget.AppCompatButton;
7+
import android.util.AttributeSet;
8+
9+
import com.github.nikartm.support.constant.Constants;
10+
11+
/**
12+
* Striped process button
13+
* @author Ivan V on 29.03.2018.
14+
* @version 1.0
15+
*/
16+
public class StripedProcessButton extends AppCompatButton implements Animatable {
17+
18+
private AnimatedStripedDrawable stripedDrawable;
19+
20+
private long startAnimDuration = Constants.NO_INIT;
21+
private long stopAnimDuration = Constants.NO_INIT;
22+
private boolean buttonAnimation = Constants.DEF_BUTTON_ANIM;
23+
private String defaultText;
24+
private String loadingText;
25+
26+
public StripedProcessButton(Context context) {
27+
super(context);
28+
initAttrs(null);
29+
}
30+
31+
public StripedProcessButton(Context context, AttributeSet attrs) {
32+
super(context, attrs);
33+
initAttrs(attrs);
34+
}
35+
36+
public StripedProcessButton(Context context, AttributeSet attrs, int defStyleAttr) {
37+
super(context, attrs, defStyleAttr);
38+
initAttrs(attrs);
39+
}
40+
41+
private void initAttrs(AttributeSet attrs) {
42+
AttributeController attrController = new AttributeController(getContext(), attrs);
43+
stripedDrawable = attrController.getStripedDrawable();
44+
loadingText = attrController.getLoadingText();
45+
}
46+
47+
@Override
48+
protected void onAttachedToWindow() {
49+
super.onAttachedToWindow();
50+
defaultText = getText() != null ? getText().toString() : "";
51+
}
52+
53+
@Override
54+
protected void onDraw(Canvas canvas) {
55+
setBackground(stripedDrawable);
56+
setEnabled(!isRunning());
57+
super.onDraw(canvas);
58+
}
59+
60+
@Override
61+
public void start() {
62+
if (isRunning()) {
63+
return;
64+
}
65+
setEnabled(isRunning());
66+
stripedDrawable.start();
67+
animateButton(isRunning());
68+
}
69+
70+
@Override
71+
public void stop() {
72+
if (!isRunning()) {
73+
return;
74+
}
75+
setEnabled(isRunning());
76+
stripedDrawable.stop();
77+
animateButton(isRunning());
78+
}
79+
80+
@Override
81+
public boolean isRunning() {
82+
return stripedDrawable.isRunning();
83+
}
84+
85+
private void animateButton(boolean start) {
86+
if (isButtonAnimation()) {
87+
long duration;
88+
if (start) {
89+
duration = startAnimDuration == Constants.NO_INIT
90+
? Constants.DEF_START_ANIM_DURATION
91+
: startAnimDuration;
92+
} else {
93+
duration = stopAnimDuration == Constants.NO_INIT
94+
? Constants.DEF_STOP_ANIM_DURATION
95+
: stopAnimDuration;
96+
}
97+
setCurrentText(start);
98+
Util.Animation.animateView(this, start, duration);
99+
}
100+
}
101+
102+
private void setCurrentText(boolean start) {
103+
String currentText;
104+
if (start) {
105+
currentText = loadingText == null ? defaultText : loadingText;
106+
} else {
107+
currentText = defaultText;
108+
}
109+
setText(currentText);
110+
}
111+
112+
public AnimatedStripedDrawable adjustButton() {
113+
return stripedDrawable;
114+
}
115+
116+
public long getStartAnimDuration() {
117+
return startAnimDuration;
118+
}
119+
120+
public StripedProcessButton setStartAnimDuration(long startAnimDuration) {
121+
this.startAnimDuration = startAnimDuration;
122+
invalidate();
123+
return this;
124+
}
125+
126+
public long getStopAnimDuration() {
127+
return stopAnimDuration;
128+
}
129+
130+
public StripedProcessButton setStopAnimDuration(long stopAnimDuration) {
131+
this.stopAnimDuration = stopAnimDuration;
132+
invalidate();
133+
return this;
134+
}
135+
136+
public String getLoadingText() {
137+
return loadingText;
138+
}
139+
140+
public StripedProcessButton setLoadingText(String loadingText) {
141+
this.loadingText = loadingText;
142+
invalidate();
143+
return this;
144+
}
145+
146+
public boolean isButtonAnimation() {
147+
return buttonAnimation;
148+
}
149+
150+
public StripedProcessButton setButtonAnimation(boolean buttonAnimation) {
151+
this.buttonAnimation = buttonAnimation;
152+
invalidate();
153+
return this;
154+
}
155+
156+
}

support/src/main/java/com/github/nikartm/support/StripedProgressButton.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

support/src/main/java/com/github/nikartm/support/constant/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.graphics.Color;
44

55
/**
6+
* Constants of stripped process button
67
* @author Ivan V on 29.03.2018.
78
* @version 1.0
89
*/
@@ -17,6 +18,11 @@ public class Constants {
1718
public static final boolean SHOW_STRIPES = true;
1819
public static final boolean GRADIENT = true;
1920

21+
public static final int NO_INIT = -1;
22+
public static final long DEF_START_ANIM_DURATION = 700;
23+
public static final long DEF_STOP_ANIM_DURATION = 300;
24+
public static final boolean DEF_BUTTON_ANIM = true;
25+
2026
public static final int DEF_BACKGROUND = Color.parseColor("#4CAF50");
2127
public static final int DEF_MAIN_STRIPE = Color.parseColor("#4CAF50");
2228
public static final int DEF_SUB_STRIPE = Color.parseColor("#CFD8DC");

0 commit comments

Comments
 (0)