Skip to content

Commit ddb5319

Browse files
committed
ProgressLoadingButton.kt added
1 parent 0763bc6 commit ddb5319

File tree

9 files changed

+244
-2
lines changed

9 files changed

+244
-2
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProgressButton/src/main/java/com/shashifreeze/progressbutton/ProgressButton.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class ProgressButton @JvmOverloads constructor(
108108
buttonProgressbar?.visibility = View.VISIBLE
109109
isEnabled = false
110110
setCardBackgroundColor(bgColor)
111+
startAnimation()
111112
}
112113

113114
fun stopProgress(afterText:String = afterProgressText, bgColor:Int = Color.GREEN ) {
@@ -116,7 +117,21 @@ class ProgressButton @JvmOverloads constructor(
116117
buttonProgressbar?.visibility = View.GONE
117118
isEnabled = true
118119
setCardBackgroundColor(bgColor)
120+
stopAnimation()
119121

120122
}
121123

124+
private fun startAnimation()
125+
{
126+
val anim = android.view.animation.AnimationUtils.loadAnimation(context,R.anim.fade_in)
127+
buttonProgressbar?.animation = anim
128+
}
129+
130+
131+
private fun stopAnimation()
132+
{
133+
val anim = android.view.animation.AnimationUtils.loadAnimation(context,R.anim.fade_out)
134+
buttonProgressbar?.animation = anim
135+
}
136+
122137
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.shashifreeze.progressbutton
2+
3+
import android.content.Context
4+
import android.content.res.ColorStateList
5+
import android.graphics.Color
6+
import android.util.AttributeSet
7+
import android.view.Gravity
8+
import android.view.LayoutInflater
9+
import android.view.View
10+
import android.view.animation.Animation
11+
import android.widget.ProgressBar
12+
import android.widget.TextView
13+
import com.google.android.material.animation.AnimationUtils
14+
import com.google.android.material.card.MaterialCardView
15+
16+
class ProgressLoadingButton @JvmOverloads constructor(
17+
context: Context,
18+
attrs: AttributeSet? = null,
19+
defStyleAttr: Int = 0
20+
) :
21+
MaterialCardView(context, attrs, defStyleAttr) {
22+
23+
private var buttonTextView:TextView? = null
24+
private var buttonProgressbar:ProgressBar? = null
25+
//private var buttonCardViewLayout:MaterialCardView? = null
26+
27+
/**
28+
* Button attributes
29+
*/
30+
private var progressColor = 0
31+
private var beforeProgressText = "Button"
32+
private var afterProgressText = "Done"
33+
private var btnTextSize = 0f
34+
private var textGravity = 0
35+
private var btnTextColor = Color.WHITE
36+
private var startProgressBgColor = Color.BLUE
37+
38+
init {
39+
40+
LayoutInflater.from(getContext()).inflate(R.layout.progress_loading_button_layout, this, true)
41+
buttonTextView = findViewById(R.id.buttonText)
42+
buttonProgressbar = findViewById(R.id.buttonProgress)
43+
// buttonCardViewLayout = findViewById(R.id.buttonCardViewLayout)
44+
45+
val typedArray = context.theme.obtainStyledAttributes(
46+
attrs,
47+
R.styleable.ProgressLoadingButtonAttr,
48+
defStyleAttr,
49+
defStyleAttr
50+
)
51+
52+
typedArray.getString(R.styleable.ProgressLoadingButtonAttr_plb_before_progress_text)?.let {
53+
beforeProgressText = it
54+
}
55+
56+
typedArray.getString(R.styleable.ProgressLoadingButtonAttr_plb_after_progress_text)?.let {
57+
afterProgressText = it
58+
}
59+
60+
typedArray.getDimension(R.styleable.ProgressLoadingButtonAttr_plb_text_size,20f).let {
61+
btnTextSize = it
62+
}
63+
64+
typedArray.getDimension(R.styleable.ProgressLoadingButtonAttr_plb_text_size,20f).let {
65+
btnTextSize = it
66+
}
67+
68+
typedArray.getInteger(R.styleable.ProgressLoadingButtonAttr_plb_text_gravity,Gravity.CENTER).let {
69+
textGravity = it
70+
}
71+
72+
typedArray.getColor(R.styleable.ProgressLoadingButtonAttr_plb_start_bg_color,Color.BLUE).let {
73+
startProgressBgColor = it
74+
}
75+
76+
typedArray.getColor(R.styleable.ProgressLoadingButtonAttr_plb_progress_color,Color.WHITE).let {
77+
progressColor = it
78+
}
79+
80+
btnTextColor = typedArray.getColor(
81+
R.styleable.ProgressLoadingButtonAttr_plb_text_color,
82+
Color.WHITE
83+
)
84+
85+
initButtonAttrs()
86+
87+
}
88+
89+
private fun initButtonAttrs() {
90+
//set button text
91+
buttonTextView?.apply {
92+
text = beforeProgressText
93+
textSize = btnTextSize
94+
gravity = textGravity
95+
setTextColor(btnTextColor)
96+
}
97+
buttonProgressbar?.indeterminateTintList = ColorStateList.valueOf(progressColor)
98+
99+
}
100+
101+
fun startProgress( bgColor:Int =startProgressBgColor ) {
102+
103+
buttonTextView?.visibility = View.GONE
104+
buttonProgressbar?.visibility = View.VISIBLE
105+
isEnabled = false
106+
setCardBackgroundColor(bgColor)
107+
startAnimation()
108+
}
109+
110+
fun stopProgress(afterText:String = afterProgressText, bgColor:Int = Color.GREEN ) {
111+
112+
buttonTextView?.text = afterText
113+
buttonTextView?.visibility = View.VISIBLE
114+
buttonProgressbar?.visibility = View.GONE
115+
isEnabled = true
116+
setCardBackgroundColor(bgColor)
117+
stopAnimation()
118+
}
119+
120+
private fun startAnimation()
121+
{
122+
val anim = android.view.animation.AnimationUtils.loadAnimation(context,R.anim.fade_in)
123+
buttonProgressbar?.animation = anim
124+
}
125+
126+
127+
private fun stopAnimation()
128+
{
129+
val anim = android.view.animation.AnimationUtils.loadAnimation(context,R.anim.fade_out)
130+
buttonProgressbar?.animation = anim
131+
}
132+
133+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:interpolator="@android:anim/accelerate_interpolator"
4+
android:duration="500">
5+
6+
<alpha android:fromAlpha="0.0"
7+
android:toAlpha="1"/>
8+
9+
</set>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:interpolator="@android:anim/accelerate_interpolator"
4+
android:duration="500">
5+
6+
<alpha android:fromAlpha="1.0"
7+
android:toAlpha="0.0"/>
8+
9+
</set>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:minHeight="35dp"
6+
android:layout_height="wrap_content">
7+
8+
<TextView
9+
android:id="@+id/buttonText"
10+
android:layout_width="0dp"
11+
android:layout_height="wrap_content"
12+
android:gravity="center"
13+
android:paddingStart="8dp"
14+
android:paddingEnd="8dp"
15+
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<ProgressBar
21+
android:id="@+id/buttonProgress"
22+
style="?android:attr/progressBarStyle"
23+
android:layout_width="24dp"
24+
android:layout_height="24dp"
25+
android:layout_marginStart="16dp"
26+
android:layout_marginEnd="16dp"
27+
android:visibility="gone"
28+
app:layout_constraintBottom_toBottomOf="parent"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintStart_toStartOf="parent"
31+
app:layout_constraintTop_toTopOf="parent" />
32+
33+
</androidx.constraintlayout.widget.ConstraintLayout>

ProgressButton/src/main/res/values/attrs.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@
1313
<attr name="pb_text_gravity" format="integer"/>
1414
</declare-styleable>
1515

16+
<declare-styleable name="ProgressLoadingButtonAttr">
17+
<attr name="plb_text_color" format="color|reference" />
18+
<attr name="plb_start_bg_color" format="color|reference" />
19+
<attr name="plb_progress_color" format="color|reference" />
20+
<attr name="plb_text_size" format="dimension|reference" />
21+
<attr name="plb_before_progress_text" format="string" />
22+
<attr name="plb_after_progress_text" format="string" />
23+
<attr name="plb_text_gravity" format="integer"/>
24+
</declare-styleable>
25+
1626
</resources>

app/src/main/java/com/shashifreeze/progressbuttonlib/MainActivity.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.os.Bundle
66
import android.os.Handler
77
import android.provider.CalendarContract
88
import com.shashifreeze.progressbutton.ProgressButton
9+
import com.shashifreeze.progressbutton.ProgressLoadingButton
910

1011
class MainActivity : AppCompatActivity() {
1112
override fun onCreate(savedInstanceState: Bundle?) {
@@ -23,5 +24,17 @@ class MainActivity : AppCompatActivity() {
2324
)
2425
}
2526

27+
val plBtn = findViewById<ProgressLoadingButton>(R.id.plb_btn)
28+
plBtn.setOnClickListener {
29+
30+
plBtn.startProgress(bgColor = Color.YELLOW)
31+
Handler(mainLooper).postDelayed(
32+
{
33+
plBtn.stopProgress("Completed")
34+
35+
}, 1000
36+
)
37+
}
38+
2639
}
2740
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@
1616
app:cardCornerRadius="6dp"
1717
app:pb_text_color = "@color/white"
1818
app:pb_text_gravity = "1"
19+
app:plb_text_size = "6sp"
1920
app:pb_progress_color = "@color/white"
20-
app:pb_text_size = "6sp"
21-
app:pb_before_progress_text = "@string/app_name"
21+
app:pb_before_progress_text = "ProgressButton"
22+
android:layout_height="wrap_content" />
23+
24+
<com.shashifreeze.progressbutton.ProgressLoadingButton
25+
android:layout_width="match_parent"
26+
android:layout_margin="10dp"
27+
android:id="@+id/plb_btn"
28+
android:gravity="center"
29+
app:cardBackgroundColor="@color/black"
30+
app:cardCornerRadius="6dp"
31+
app:plb_text_color = "@color/white"
32+
app:plb_text_gravity = "1"
33+
app:plb_progress_color = "@color/black"
34+
app:plb_text_size = "6sp"
35+
app:plb_before_progress_text = "@string/app_name"
2236
android:layout_height="wrap_content" />
2337

2438
</LinearLayout>

0 commit comments

Comments
 (0)