Instantly share code, notes, and snippets.
Last active
February 4, 2023 19:18
-
Star
(2)
2
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save ThanosFisherman/8cb9f24e7e9d7d97420e7f8832dd738b to your computer and use it in GitHub Desktop.
It's a checkable ImageButton that respects xml selector state changes
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
/** | |
* Created by thanos on 12/8/17. | |
* It's a checkable ImageButton that respects xml selector state changes | |
*/ | |
public final class CheckableImageButton extends AppCompatImageButton implements Checkable | |
{ | |
private static final int[] DRAWABLE_STATE_CHECKED = new int[]{android.R.attr.state_checked}; | |
private boolean mChecked; | |
public CheckableImageButton(Context context) | |
{ | |
this(context, null); | |
} | |
public CheckableImageButton(Context context, AttributeSet attrs) | |
{ | |
this(context, attrs, android.support.v7.appcompat.R.attr.imageButtonStyle); | |
} | |
public CheckableImageButton(Context context, AttributeSet attrs, int defStyleAttr) | |
{ | |
super(context, attrs, defStyleAttr); | |
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegateCompat() | |
{ | |
@Override | |
public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) | |
{ | |
super.onInitializeAccessibilityEvent(host, event); | |
event.setChecked(isChecked()); | |
} | |
@Override | |
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) | |
{ | |
super.onInitializeAccessibilityNodeInfo(host, info); | |
info.setCheckable(true); | |
info.setChecked(isChecked()); | |
} | |
}); | |
} | |
@Override | |
public void setChecked(boolean checked) | |
{ | |
if (mChecked != checked) | |
{ | |
mChecked = checked; | |
refreshDrawableState(); | |
sendAccessibilityEvent(AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED); | |
} | |
} | |
@Override | |
public boolean isChecked() | |
{ | |
return mChecked; | |
} | |
@Override | |
public void toggle() | |
{ | |
setChecked(!mChecked); | |
} | |
@Override | |
public boolean performClick() | |
{ | |
toggle(); | |
return super.performClick(); | |
} | |
@Override | |
public int[] onCreateDrawableState(int extraSpace) | |
{ | |
if (mChecked) | |
return mergeDrawableStates(super.onCreateDrawableState(extraSpace + DRAWABLE_STATE_CHECKED.length), DRAWABLE_STATE_CHECKED); | |
else | |
return super.onCreateDrawableState(extraSpace); | |
} | |
@Override | |
protected Parcelable onSaveInstanceState() | |
{ | |
CheckableImageButton.SavedState result = new CheckableImageButton.SavedState(super.onSaveInstanceState()); | |
result.checked = mChecked; | |
return result; | |
} | |
@Override | |
protected void onRestoreInstanceState(Parcelable state) | |
{ | |
if (!(state instanceof CheckableImageButton.SavedState)) | |
{ | |
super.onRestoreInstanceState(state); | |
return; | |
} | |
CheckableImageButton.SavedState ss = (CheckableImageButton.SavedState) state; | |
super.onRestoreInstanceState(ss.getSuperState()); | |
setChecked(ss.checked); | |
} | |
protected static class SavedState extends BaseSavedState | |
{ | |
boolean checked; | |
SavedState(Parcelable superState) | |
{ | |
super(superState); | |
} | |
@Override | |
public void writeToParcel(Parcel out, int flags) | |
{ | |
super.writeToParcel(out, flags); | |
out.writeInt(checked ? 1 : 0); | |
} | |
public static final Creator<CheckableImageButton.SavedState> CREATOR = new Creator<CheckableImageButton.SavedState>() | |
{ | |
public CheckableImageButton.SavedState createFromParcel(Parcel in) | |
{ | |
return new CheckableImageButton.SavedState(in); | |
} | |
public CheckableImageButton.SavedState[] newArray(int size) | |
{ | |
return new CheckableImageButton.SavedState[size]; | |
} | |
}; | |
private SavedState(Parcel in) | |
{ | |
super(in); | |
checked = in.readInt() == 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment