Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 23391b3

Browse files
committed
added success and error state
1 parent ecfc982 commit 23391b3

8 files changed

Lines changed: 152 additions & 40 deletions

File tree

.idea/assetWizardSettings.xml

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

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/droidbond/loadingbuttonsample/MainActivity.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ class MainActivity : AppCompatActivity() {
1818
custombtn.showLoading()
1919
normal.showLoading()
2020
} else {
21-
custombtn.hideLoading()
22-
normal.hideLoading()
21+
if(custombtn.isLoading()) {
22+
custombtn.showSuccess()
23+
normal.showError()
24+
}else{
25+
custombtn.hideLoading()
26+
normal.hideLoading()
27+
show = !show
28+
}
2329
}
24-
25-
show = !show
2630
}
2731

2832
custombtn.setOnClickListener {

loadingbutton/src/main/java/com/droidbond/loadingbutton/LoadingButton.kt

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import android.content.res.Resources
55
import android.content.res.TypedArray
66
import android.graphics.PorterDuff
77
import android.graphics.Typeface
8+
import android.os.Build
89
import android.support.annotation.NonNull
910
import android.support.annotation.Nullable
1011
import android.support.v4.content.res.ResourcesCompat
12+
import android.transition.Fade
13+
import android.transition.TransitionManager
1114
import android.util.AttributeSet
1215
import android.util.Log
1316
import android.view.Gravity
1417
import android.view.View
18+
import android.view.ViewGroup
1519
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
1620
import android.widget.FrameLayout
21+
import android.widget.ImageView
1722
import android.widget.ProgressBar
1823
import android.widget.TextView
1924

@@ -24,88 +29,119 @@ class LoadingButton @JvmOverloads constructor(
2429

2530
private val TAG = "LoadingButton"
2631

27-
private var progressBar: ProgressBar? = null
28-
private var tvText: TextView? = null
29-
private var array: TypedArray? = null
32+
private lateinit var progressBar: ProgressBar
33+
private lateinit var img: ImageView
34+
private lateinit var view: View
35+
private lateinit var tvText: TextView
36+
private var array: TypedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.app, defStyleAttr, 0)
37+
private var bg: Int = R.drawable.ic_bg_blue_buttons_style
3038

3139
init {
32-
array = context.theme.obtainStyledAttributes(attrs, R.styleable.app, defStyleAttr, 0)
3340
initView()
3441
}
3542

3643
fun hideLoading() {
37-
if (progressBar != null) {
38-
progressBar!!.visibility = View.INVISIBLE
39-
}
40-
41-
if (tvText != null) {
42-
tvText!!.visibility = View.VISIBLE
43-
}
44+
view.setBackgroundResource(bg)
45+
progressBar.visibility = View.INVISIBLE
46+
img.visibility = View.INVISIBLE
47+
tvText.visibility = View.VISIBLE
4448
Log.d(TAG, "stopLoading: ")
4549
}
4650

47-
fun showLoading() {
48-
if (progressBar != null) {
49-
progressBar!!.visibility = View.VISIBLE
51+
fun showSuccess() {
52+
tvText.visibility = View.INVISIBLE
53+
view.setBackgroundResource(bg)
54+
55+
if (Build.VERSION.SDK_INT >= 19) {
56+
TransitionManager.beginDelayedTransition(view as ViewGroup, Fade(Fade.IN))
5057
}
58+
progressBar.visibility = View.INVISIBLE
59+
img.visibility = View.VISIBLE
60+
img.setImageResource(R.drawable.ic_done_white_24dp)
61+
}
62+
63+
fun showError() {
64+
tvText.visibility = View.INVISIBLE
65+
66+
view.setBackgroundResource(R.drawable.ic_bg_red_buttons_style)
5167

52-
if (tvText != null) {
53-
tvText!!.visibility = View.INVISIBLE
68+
if (Build.VERSION.SDK_INT >= 19) {
69+
TransitionManager.beginDelayedTransition(view as ViewGroup, Fade(Fade.IN))
5470
}
71+
72+
progressBar.visibility = View.INVISIBLE
73+
img.visibility = View.VISIBLE
74+
img.setImageResource(R.drawable.ic_warning)
75+
}
76+
77+
fun showLoading() {
78+
view.setBackgroundResource(bg)
79+
progressBar.visibility = View.VISIBLE
80+
tvText.visibility = View.INVISIBLE
81+
img.visibility = View.INVISIBLE
5582
Log.d(TAG, "startLoading: ")
5683
}
5784

85+
fun isLoading():Boolean{
86+
return progressBar.isShown
87+
}
88+
5889
private fun initView() {
5990

60-
val view = inflate(context, R.layout.layout_loading_button_lb, null)
91+
view = inflate(context, R.layout.layout_loading_button_lb, null)
6192
addView(view)
6293

63-
val bg = array?.getResourceId(R.styleable.app_background, 0)
94+
val bg = array.getResourceId(R.styleable.app_background, 0)
6495

6596
if (bg != 0) {
66-
view.setBackgroundResource(bg!!)
97+
view.setBackgroundResource(bg)
98+
this.bg = bg
6799
}
68100

69101
progressBar = view.findViewById(R.id.pb)
102+
img = view.findViewById(R.id.img)
103+
70104
tvText = view.findViewById(R.id.tvText)
71105

72-
progressBar?.indeterminateDrawable?.setColorFilter(
73-
array!!.getColor(R.styleable.app_progressColor, resources.getColor(R.color.white)),
106+
progressBar.indeterminateDrawable?.setColorFilter(
107+
array.getColor(R.styleable.app_progressColor, resources.getColor(R.color.white)),
74108
PorterDuff.Mode.SRC_ATOP
75109
)
76110

77-
var pbSize: Int = array?.getInteger(R.styleable.app_progressBarSize, 32)!!
111+
var pbSize: Int = array.getInteger(R.styleable.app_progressBarSize, 32)
78112
pbSize = Math.round(pbSize * (Resources.getSystem().displayMetrics.density))
79113

80114
val pbParams = LayoutParams(WRAP_CONTENT, pbSize)
81115
pbParams.gravity = Gravity.CENTER
82-
progressBar?.layoutParams = pbParams
116+
progressBar.layoutParams = pbParams
83117

84118

85119
var text = "Loading Button"
86-
if (array!!.getText(R.styleable.app_text) != null) {
87-
text = array!!.getText(R.styleable.app_text).toString()
120+
if (array.getText(R.styleable.app_text) != null) {
121+
text = array.getText(R.styleable.app_text).toString()
88122
}
89123

90-
tvText!!.text = text
91-
tvText!!.setTextColor(array!!.getColor(R.styleable.app_textColor, resources.getColor(R.color.white)))
124+
tvText.text = text
125+
tvText.setTextColor(array.getColor(R.styleable.app_textColor, resources.getColor(R.color.white)))
92126

93-
if (array!!.getBoolean(R.styleable.app_boldText, false)) {
94-
tvText!!.setTypeface(null, Typeface.BOLD)
127+
if (array.getBoolean(R.styleable.app_boldText, false)) {
128+
tvText.setTypeface(null, Typeface.BOLD)
95129
}
96130

97-
var size = array!!.getDimension(R.styleable.app_textSize, 14f * Resources.getSystem().displayMetrics.density)
131+
var size = array.getDimension(R.styleable.app_textSize, 14f * Resources.getSystem().displayMetrics.density)
98132
size /= Resources.getSystem().displayMetrics.density
99133

100-
tvText?.textSize = size
134+
tvText.textSize = size
101135

102-
if (array!!.hasValue(R.styleable.app_customFontFamily)) {
103-
val fontId = array!!.getResourceId(R.styleable.app_customFontFamily, -1)
136+
if (array.hasValue(R.styleable.app_customFontFamily)) {
137+
val fontId = array.getResourceId(R.styleable.app_customFontFamily, -1)
104138
val typeface = ResourcesCompat.getFont(context, fontId)
105-
tvText?.typeface = typeface
139+
tvText.typeface = typeface
140+
} else {
141+
tvText.typeface = null
106142
}
107143

108-
array?.recycle()
144+
array.recycle()
109145
}
110146

111147
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:autoMirrored="true" android:height="24dp"
2+
android:viewportHeight="44" android:viewportWidth="312"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#ffff4444" android:fillType="evenOdd" android:pathData="M0,3C0,1.343 1.343,0 3,0H309C310.657,0 312,1.343 312,3V41C312,42.657 310.657,44 309,44H3C1.343,44 0,42.657 0,41V3Z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
5+
</vector>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<vector android:height="24dp" android:viewportHeight="486.463"
2+
android:viewportWidth="486.463" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#FFFFFF" android:pathData="M243.225,333.382c-13.6,0 -25,11.4 -25,25s11.4,25 25,25c13.1,0 25,-11.4 24.4,-24.4C268.225,344.682 256.925,333.382 243.225,333.382z"/>
4+
<path android:fillColor="#FFFFFF" android:pathData="M474.625,421.982c15.7,-27.1 15.8,-59.4 0.2,-86.4l-156.6,-271.2c-15.5,-27.3 -43.5,-43.5 -74.9,-43.5s-59.4,16.3 -74.9,43.4l-156.8,271.5c-15.6,27.3 -15.5,59.8 0.3,86.9c15.6,26.8 43.5,42.9 74.7,42.9h312.8C430.725,465.582 458.825,449.282 474.625,421.982zM440.625,402.382c-8.7,15 -24.1,23.9 -41.3,23.9h-312.8c-17,0 -32.3,-8.7 -40.8,-23.4c-8.6,-14.9 -8.7,-32.7 -0.1,-47.7l156.8,-271.4c8.5,-14.9 23.7,-23.7 40.9,-23.7c17.1,0 32.4,8.9 40.9,23.8l156.7,271.4C449.325,369.882 449.225,387.482 440.625,402.382z"/>
5+
<path android:fillColor="#FFFFFF" android:pathData="M237.025,157.882c-11.9,3.4 -19.3,14.2 -19.3,27.3c0.6,7.9 1.1,15.9 1.7,23.8c1.7,30.1 3.4,59.6 5.1,89.7c0.6,10.2 8.5,17.6 18.7,17.6c10.2,0 18.2,-7.9 18.7,-18.2c0,-6.2 0,-11.9 0.6,-18.2c1.1,-19.3 2.3,-38.6 3.4,-57.9c0.6,-12.5 1.7,-25 2.3,-37.5c0,-4.5 -0.6,-8.5 -2.3,-12.5C260.825,160.782 248.925,155.082 237.025,157.882z"/>
6+
</vector>

loadingbutton/src/main/res/layout/layout_loading_button_lb.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@
2222
android:layout_gravity="center"
2323
android:visibility="invisible"
2424
tools:indeterminateTint="@color/white"/>
25+
26+
<ImageView
27+
android:id="@+id/img"
28+
android:layout_height="wrap_content"
29+
android:layout_width="wrap_content"
30+
android:layout_gravity="center"
31+
android:src="@drawable/ic_done_white_24dp"
32+
android:visibility="invisible"/>
2533
</FrameLayout>

0 commit comments

Comments
 (0)