Last active
September 30, 2016 18:42
-
-
Save jchernandez/567c1f2f09ce286b396d48f14fd0a43e to your computer and use it in GitHub Desktop.
Tinted Progress Bar
This file contains hidden or 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
/*** | |
This progressbar is tinted with the accent color of an application | |
set a color filter to the progressbar drawable | |
so change the color of the progressbar in pre-lollipop versions | |
**/ | |
package com.urbanity.wallet.ui.components; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.graphics.PorterDuff; | |
import android.os.Build; | |
import android.support.annotation.ColorInt; | |
import android.support.v4.content.ContextCompat; | |
import android.util.AttributeSet; | |
import android.util.TypedValue; | |
import android.widget.ProgressBar; | |
import com.urbanity.wallet.R; | |
public class TintedProgressBar extends ProgressBar { | |
public TintedProgressBar(Context context) { | |
super(context); | |
init(); | |
} | |
public TintedProgressBar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public TintedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public TintedProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
public void init(){ | |
int accentColor; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
accentColor = android.R.attr.colorAccent; | |
} else { | |
accentColor = R.attr.colorAccent; | |
} | |
this.getIndeterminateDrawable().mutate() | |
.setColorFilter(resolveColor(accentColor), | |
PorterDuff.Mode.SRC_ATOP); | |
} | |
/** | |
* Resolve a color data from an attribute | |
* @param attr Attribute color to resolve | |
* @return int resolved color. | |
*/ | |
@ColorInt | |
private int resolveColor(int attr) { | |
TypedValue a = new TypedValue(); | |
getContext().getTheme().resolveAttribute(attr, a, true); | |
return a.data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment