Instantly share code, notes, and snippets.
Created
May 22, 2015 17:40
-
Save eyecreate/702d163476f6a7e1cc18 to your computer and use it in GitHub Desktop.
Did you want an imagebutton when using support lib? Now you can have one.
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.content.Context; | |
import android.content.res.ColorStateList; | |
import android.graphics.PorterDuff; | |
import android.support.annotation.Nullable; | |
import android.support.v4.view.TintableBackgroundView; | |
import android.support.v7.appcompat.R; | |
import android.support.v7.internal.widget.TintInfo; | |
import android.support.v7.internal.widget.TintManager; | |
import android.support.v7.internal.widget.TintTypedArray; | |
import android.util.AttributeSet; | |
import android.view.accessibility.AccessibilityEvent; | |
import android.view.accessibility.AccessibilityNodeInfo; | |
import android.widget.ImageButton; | |
/** | |
* A tint aware {@link android.widget.ImageButton}. | |
* <p> | |
* This will automatically be used when you use {@link android.widget.ImageButton} in your layouts. You | |
* should only need to manually use this class when writing custom views. | |
*/ | |
public class AppCompatImageButton extends ImageButton implements TintableBackgroundView { | |
private static final int[] TINT_ATTRS = { | |
android.R.attr.background | |
}; | |
private TintInfo mBackgroundTint; | |
public AppCompatImageButton(Context context) { | |
this(context, null); | |
} | |
public AppCompatImageButton(Context context, AttributeSet attrs) { | |
this(context, attrs, R.attr.buttonStyle); | |
} | |
public AppCompatImageButton(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
if (TintManager.SHOULD_BE_USED) { | |
TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, | |
TINT_ATTRS, defStyleAttr, 0); | |
if (a.hasValue(0)) { | |
ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); | |
if (tint != null) { | |
setSupportBackgroundTintList(tint); | |
} | |
} | |
a.recycle(); | |
} | |
} | |
/** | |
* This should be accessed via | |
* {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, | |
* android.content.res.ColorStateList)} | |
* | |
* @hide | |
*/ | |
@Override | |
public void setSupportBackgroundTintList(@Nullable ColorStateList tint) { | |
if (mBackgroundTint == null) { | |
mBackgroundTint = new TintInfo(); | |
} | |
mBackgroundTint.mTintList = tint; | |
mBackgroundTint.mHasTintList = true; | |
applySupportBackgroundTint(); | |
} | |
/** | |
* This should be accessed via | |
* {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)} | |
* | |
* @hide | |
*/ | |
@Override | |
@Nullable | |
public ColorStateList getSupportBackgroundTintList() { | |
return mBackgroundTint != null ? mBackgroundTint.mTintList : null; | |
} | |
/** | |
* This should be accessed via | |
* {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, android.graphics.PorterDuff.Mode)} | |
* | |
* @hide | |
*/ | |
@Override | |
public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { | |
if (mBackgroundTint == null) { | |
mBackgroundTint = new TintInfo(); | |
} | |
mBackgroundTint.mTintMode = tintMode; | |
mBackgroundTint.mHasTintMode = true; | |
applySupportBackgroundTint(); | |
} | |
/** | |
* This should be accessed via | |
* {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)} | |
* | |
* @hide | |
*/ | |
@Override | |
@Nullable | |
public PorterDuff.Mode getSupportBackgroundTintMode() { | |
return mBackgroundTint != null ? mBackgroundTint.mTintMode : null; | |
} | |
@Override | |
protected void drawableStateChanged() { | |
super.drawableStateChanged(); | |
applySupportBackgroundTint(); | |
} | |
private void applySupportBackgroundTint() { | |
if (getBackground() != null && mBackgroundTint != null) { | |
TintManager.tintViewBackground(this, mBackgroundTint); | |
} | |
} | |
@Override | |
public void onInitializeAccessibilityEvent(AccessibilityEvent event) { | |
super.onInitializeAccessibilityEvent(event); | |
event.setClassName(ImageButton.class.getName()); | |
} | |
@Override | |
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { | |
super.onInitializeAccessibilityNodeInfo(info); | |
info.setClassName(ImageButton.class.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment