Created
January 23, 2019 15:30
-
-
Save DerTyp7214/85676a4acffe138870e8258d7d830826 to your computer and use it in GitHub Desktop.
Custom Progressbar for Android Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.animation.ValueAnimator | |
import android.content.Context | |
import android.graphics.Color | |
import android.graphics.PorterDuff | |
import android.os.Build | |
import android.util.AttributeSet | |
import android.util.DisplayMetrics | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT | |
import android.view.animation.DecelerateInterpolator | |
import android.widget.ProgressBar | |
import androidx.annotation.ColorInt | |
class ColoredProgressBar : ProgressBar { | |
constructor(context: Context) : this(context, null) | |
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) | |
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { | |
init() | |
} | |
private fun init() { | |
progressDrawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY) | |
} | |
override fun onAttachedToWindow() { | |
super.onAttachedToWindow() | |
val height = convertDpToPixel(4F, context).toInt() | |
if (layoutParams != null) { | |
layoutParams.height = height | |
} else { | |
layoutParams = ViewGroup.LayoutParams(WRAP_CONTENT, height) | |
} | |
if (hideAtMax && progress == 0) visibility = GONE | |
requestLayout() | |
} | |
override fun setProgress(progress: Int) { | |
super.setProgress(progress) | |
visibility = if (hideAtMax && progress == max) GONE | |
else if (hideAtMax && progress == 0) GONE | |
else View.VISIBLE | |
} | |
override fun setProgress(progress: Int, animate: Boolean) { | |
when { | |
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> super.setProgress(progress, animate) | |
animate -> { | |
val animator = ValueAnimator.ofInt(progress, progress) | |
animator.addUpdateListener { | |
this.progress = it.animatedValue as Int | |
} | |
animator.duration = 80 | |
animator.interpolator = DecelerateInterpolator() | |
animator.start() | |
} | |
else -> this.progress = progress | |
} | |
visibility = if (hideAtMax && progress == max) GONE | |
else if (hideAtMax && progress == 0) GONE | |
else View.VISIBLE | |
} | |
/** | |
* Set margins to the layoutParams | |
* <p> | |
* @param left margin Left/Start | |
* @param top margin Top | |
* @param right margin Right/End | |
* @param bottom margin Bottom | |
*/ | |
fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { | |
if (layoutParams is ViewGroup.MarginLayoutParams) { | |
val p = layoutParams as ViewGroup.MarginLayoutParams | |
p.setMargins(left, top, right, bottom) | |
requestLayout() | |
} | |
} | |
/** | |
* Set the color of the ProgressBar | |
* <p> | |
* @param color the color for the ProgressBar | |
*/ | |
fun setColor(@ColorInt color: Int) { | |
progressDrawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY) | |
} | |
/** | |
* If true it will hide the ProgressBar atomatically on 0 and max percentage | |
* <p> | |
*/ | |
var hideAtMax: Boolean = false | |
private fun convertDpToPixel(dp: Float, context: Context): Float { | |
return dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment