Skip to content

Instantly share code, notes, and snippets.

@dekalo-stanislav
Forked from akshaydashrath/ToggleImageButton
Last active December 15, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekalo-stanislav/118502f46bcbe1a5ae4e to your computer and use it in GitHub Desktop.
Save dekalo-stanislav/118502f46bcbe1a5ae4e to your computer and use it in GitHub Desktop.
A ToggleImageButton for Android
<resources>
<declare-styleable name="ToogleImageButton">
<attr name="checked" format="boolean"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageButton;
public class ToggleImageButton extends ImageButton implements Checkable {
private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_checked
};
private boolean isChecked;
private boolean isBroadCasting;
private OnCheckedChangeListener onCheckedChangeListener;
public ToggleImageButton(Context context) {
super( context );
}
public ToggleImageButton(Context context, AttributeSet attrs) {
super( context, attrs );
}
public ToggleImageButton(Context context, AttributeSet attrs, int defStyle) {
super( context, attrs, defStyle );
initAttr( context, attrs, defStyle );
}
private void initAttr(Context context, AttributeSet attrs, int defStyle) {
TypedArray a =
context.obtainStyledAttributes(
attrs, R.styleable.ToogleImageButton, defStyle, 0 );
boolean checked = a
.getBoolean( R.styleable.ToogleImageButton_checked, false );
setChecked( checked );
a.recycle();
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
@Override
public boolean isChecked() {
return isChecked; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void setChecked(boolean checked) {
if (isChecked == checked) {
return;
}
isChecked = checked;
refreshDrawableState();
if (isBroadCasting) {
return;
}
isBroadCasting = true;
if (onCheckedChangeListener != null) {
onCheckedChangeListener.onCheckedChanged( this, isChecked );
}
isBroadCasting = false;
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState( extraSpace + 1 );
if (isChecked()) {
mergeDrawableStates( drawableState, CHECKED_STATE_SET );
}
return drawableState;
}
public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
}
@Override
public void toggle() {
setChecked( !isChecked );
}
@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable( "instanceState", super.onSaveInstanceState() );
bundle.putBoolean( "state", isChecked );
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle outState = (Bundle) state;
setChecked( outState.getBoolean( "state" ) );
state = outState.getParcelable( "instanceState" );
}
super.onRestoreInstanceState( state );
}
public static interface OnCheckedChangeListener {
void onCheckedChanged(ToggleImageButton buttonView, boolean isChecked);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment