Skip to content

Instantly share code, notes, and snippets.

@anitasantoso
Last active August 29, 2015 14:05
Show Gist options
  • Save anitasantoso/4d8d6f546c4a480ae4d0 to your computer and use it in GitHub Desktop.
Save anitasantoso/4d8d6f546c4a480ae4d0 to your computer and use it in GitHub Desktop.
package com.isobar.operationlife.ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageButton;
import com.isobar.operationlife.R;
/**
* Created by anitasantoso on 18/07/2014.
*/
public class StatefulImageButton extends ImageButton {
Drawable disabledSrc;
Drawable enabledSrc;
public StatefulImageButton(final Context context) {
this(context, null);
}
public StatefulImageButton(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public StatefulImageButton(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.StateImageButton, defStyle, 0);
disabledSrc = a.getDrawable(R.styleable.StateImageButton_disabledSrc);
enabledSrc = a.getDrawable(R.styleable.StateImageButton_enabledSrc);
if(enabledSrc == null) {
enabledSrc = getDrawable();
}
a.recycle();
}
/**
* Set disabled drawable if specified
* @param enabled
*/
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
setImageDrawable(isEnabled()? enabledSrc : (disabledSrc != null? disabledSrc : enabledSrc));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment