Skip to content

Instantly share code, notes, and snippets.

@creativepsyco
Created October 4, 2013 06:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativepsyco/6821592 to your computer and use it in GitHub Desktop.
Save creativepsyco/6821592 to your computer and use it in GitHub Desktop.
HighlightRadionButton with two state list drawable
public class HighlightedImageRadioButton extends RadioButton {
private Drawable buttonDrawable;
public HighlightedImageRadioButton(Context context) {
super(context);
}
public HighlightedImageRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
initHighlightedImageRadioButton(context, attrs, 0);
}
public HighlightedImageRadioButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initHighlightedImageRadioButton(context, attrs, defStyle);
}
private void initHighlightedImageRadioButton(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HighlightedImageRadioButton, defStyle, 0);
if (a == null) return;
Drawable normalDrawable = a.getDrawable(R.styleable.HighlightedImageRadioButton_normalDrawable);
Drawable focusedDrawable = a.getDrawable(R.styleable.HighlightedImageRadioButton_focusedDrawable);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focusedDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, focusedDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, focusedDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_selected}, focusedDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, normalDrawable);
buttonDrawable = stateListDrawable;
setButtonDrawable(R.drawable.beetalk_common_buzz_transparent_bg);
}
/**
* Fix for putting the drawable in the center
* notice that we put the background color of the drawable to transparent
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (buttonDrawable != null) {
buttonDrawable.setState(getDrawableState());
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();
int y = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
int buttonWidth = buttonDrawable.getIntrinsicWidth();
int buttonLeft = (getWidth() - buttonWidth) / 2;
buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height);
buttonDrawable.draw(canvas);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
buttonDrawable = null;
}
}
@jackyhieu1211
Copy link

not working correct in android 7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment