Created
April 1, 2015 02:28
-
-
Save NightlyNexus/cca0e2703fbe14525473 to your computer and use it in GitHub Desktop.
TintedProgressBar
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
<?xml version="1.0" encoding="utf-8"?> | |
<declare-styleable name="TintedProgressBar"> | |
<attr name="tint" format="color|reference" /> | |
</declare-styleable> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<com.ifttt.lib.TintedProgressBar | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
style="@style/Widget.AppCompat.ProgressBar.Horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:indeterminate="true" | |
app:tint="@color/ifttt_blue_light" /> |
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.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.PorterDuff; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.widget.ProgressBar; | |
public class TintedProgressBar extends ProgressBar { | |
public TintedProgressBar(Context context) { | |
super(context); | |
init(context, null, 0); | |
} | |
public TintedProgressBar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs, 0); | |
} | |
public TintedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public TintedProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(context, attrs, defStyleAttr); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyle) { | |
final TypedArray a = context.obtainStyledAttributes(attrs, | |
R.styleable.TintedProgressBar, defStyle, 0); | |
if (a == null) { | |
return; | |
} | |
if (a.hasValue(R.styleable.TintedProgressBar_tint)) { | |
final int tintColor = a.getColor(R.styleable.TintedProgressBar_tint, 0); | |
getIndeterminateDrawable().setColorFilter(tintColor, PorterDuff.Mode.SRC_IN); | |
getProgressDrawable().setColorFilter(tintColor, PorterDuff.Mode.SRC_IN); | |
} | |
a.recycle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When we use the TintedProgressBar and AppCompat 23.4.0 with
style="@style/Widget.AppCompat.ProgressBar"
(instead ofWidget.AppCompat.ProgressBar. Horizontal
) it will crash.Why? Because
getProgressDrawable()
orgetIndeterminateDrawable()
can benull
.Fix: