Fixed tinting on pre-lollipop devices for custom widgets (#481)
This commit is contained in:
parent
fa6796475c
commit
ce664a08f8
|
@ -19,12 +19,14 @@ import android.content.Context;
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.support.v7.widget.AppCompatImageButton;
|
import android.support.v7.widget.AppCompatImageButton;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.view.ViewTreeObserver;
|
||||||
|
|
||||||
import org.xbmc.kore.R;
|
import org.xbmc.kore.R;
|
||||||
|
import org.xbmc.kore.utils.Utils;
|
||||||
|
|
||||||
public class HighlightButton extends AppCompatImageButton {
|
public class HighlightButton extends AppCompatImageButton {
|
||||||
|
|
||||||
private int colorFilter;
|
private int highlightColorFilter;
|
||||||
|
|
||||||
private boolean highlight;
|
private boolean highlight;
|
||||||
|
|
||||||
|
@ -36,16 +38,18 @@ public class HighlightButton extends AppCompatImageButton {
|
||||||
public HighlightButton(Context context, AttributeSet attrs) {
|
public HighlightButton(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
setStyle(context);
|
setStyle(context);
|
||||||
|
fixThemeColorForPreLollipop(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HighlightButton(Context context, AttributeSet attrs, int defStyle) {
|
public HighlightButton(Context context, AttributeSet attrs, int defStyle) {
|
||||||
super(context, attrs, defStyle);
|
super(context, attrs, defStyle);
|
||||||
setStyle(context);
|
setStyle(context);
|
||||||
|
fixThemeColorForPreLollipop(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHighlight(boolean highlight) {
|
public void setHighlight(boolean highlight) {
|
||||||
if (highlight) {
|
if (highlight) {
|
||||||
setColorFilter(colorFilter);
|
setColorFilter(highlightColorFilter);
|
||||||
} else {
|
} else {
|
||||||
clearColorFilter();
|
clearColorFilter();
|
||||||
}
|
}
|
||||||
|
@ -58,11 +62,40 @@ public class HighlightButton extends AppCompatImageButton {
|
||||||
|
|
||||||
private void setStyle(Context context) {
|
private void setStyle(Context context) {
|
||||||
if (!this.isInEditMode()) {
|
if (!this.isInEditMode()) {
|
||||||
TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
|
TypedArray styledAttributes =
|
||||||
new int[]{R.attr.colorAccent});
|
context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
|
||||||
colorFilter = styledAttributes.getColor(styledAttributes.getIndex(0),
|
highlightColorFilter = styledAttributes.getColor(styledAttributes.getIndex(0),
|
||||||
context.getResources().getColor(R.color.accent_default));
|
context.getResources().getColor(R.color.accent_default));
|
||||||
styledAttributes.recycle();
|
styledAttributes.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hack!
|
||||||
|
* Tinting is not applied on pre-lollipop devices.
|
||||||
|
* As there is (AFAICT) no proper way to set this manually we simply
|
||||||
|
* apply the color filter each time the view has been laid out.
|
||||||
|
* @param context
|
||||||
|
*/
|
||||||
|
private void fixThemeColorForPreLollipop(Context context) {
|
||||||
|
if (Utils.isLollipopOrLater() || this.isInEditMode())
|
||||||
|
return;
|
||||||
|
|
||||||
|
TypedArray styledAttributes =
|
||||||
|
context.getTheme().obtainStyledAttributes(new int[]{R.attr.defaultButtonColorFilter});
|
||||||
|
final int colorFilter = styledAttributes.getColor(styledAttributes.getIndex(0),
|
||||||
|
context.getResources().getColor(R.color.white));
|
||||||
|
styledAttributes.recycle();
|
||||||
|
|
||||||
|
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||||
|
@Override
|
||||||
|
public void onGlobalLayout() {
|
||||||
|
if (highlight) {
|
||||||
|
setColorFilter(highlightColorFilter);
|
||||||
|
} else {
|
||||||
|
setColorFilter(colorFilter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ import android.util.AttributeSet;
|
||||||
|
|
||||||
import org.xbmc.kore.R;
|
import org.xbmc.kore.R;
|
||||||
|
|
||||||
public class RepeatModeButton extends AppCompatImageButton {
|
public class RepeatModeButton extends HighlightButton {
|
||||||
public enum MODE {
|
public enum MODE {
|
||||||
OFF,
|
OFF,
|
||||||
ONE,
|
ONE,
|
||||||
|
@ -32,7 +32,6 @@ public class RepeatModeButton extends AppCompatImageButton {
|
||||||
|
|
||||||
private MODE mode;
|
private MODE mode;
|
||||||
private static TypedArray styledAttributes;
|
private static TypedArray styledAttributes;
|
||||||
private static int accentDefaultColor;
|
|
||||||
|
|
||||||
public RepeatModeButton(Context context) {
|
public RepeatModeButton(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
@ -55,15 +54,15 @@ public class RepeatModeButton extends AppCompatImageButton {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case OFF:
|
case OFF:
|
||||||
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_repeat_white_24dp));
|
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_repeat_white_24dp));
|
||||||
clearColorFilter();
|
setHighlight(false);
|
||||||
break;
|
break;
|
||||||
case ONE:
|
case ONE:
|
||||||
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(2), R.drawable.ic_repeat_one_white_24dp));
|
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(2), R.drawable.ic_repeat_one_white_24dp));
|
||||||
setColorFilter(styledAttributes.getColor(styledAttributes.getIndex(0), accentDefaultColor));
|
setHighlight(true);
|
||||||
break;
|
break;
|
||||||
case ALL:
|
case ALL:
|
||||||
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_repeat_white_24dp));
|
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_repeat_white_24dp));
|
||||||
setColorFilter(styledAttributes.getColor(styledAttributes.getIndex(0), accentDefaultColor));
|
setHighlight(true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,6 +76,5 @@ public class RepeatModeButton extends AppCompatImageButton {
|
||||||
R.attr.colorAccent,
|
R.attr.colorAccent,
|
||||||
R.attr.iconRepeat,
|
R.attr.iconRepeat,
|
||||||
R.attr.iconRepeatOne});
|
R.attr.iconRepeatOne});
|
||||||
accentDefaultColor = getContext().getResources().getColor(R.color.accent_default);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue