title: Android custom CheckableImageButton not working correctly
body: <p>I want to use widget as <code>CheckableImageButton</code> on my application, but this code, image state not working correctly when I click on image that state must be changed</p>
  
  <p>always I get true after change state</p>
  
  <pre><code>public class CheckableImageButton extends ImageButton implements Checkable {
  
      private boolean mChecked;
      private boolean mBroadcasting;
      private int mPersonality;
      private OnCheckedChangeListener mOnCheckedChangeListener;
  
      private static final int[] CHECKED_STATE_SET = { R.attr.is_checked };
  
      private static final int PERSONALITY_RADIO_BUTTON = 0;
      private static final int PERSONALITY_CHECK_BOX = 1;
  
      public CheckableImageButton(Context context) {
          this(context, null);
      }
  
      public CheckableImageButton(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckableImageButton);
  
          mPersonality = a.getInt(R.styleable.CheckableImageButton_personality, PERSONALITY_RADIO_BUTTON);
          boolean checked = a.getBoolean(R.styleable.CheckableImageButton_is_checked, false);
          setChecked(checked);
  
          a.recycle();
      }
  
      public void toggle() {
          setChecked(!mChecked);
      }
  
      @Override
      public boolean performClick() {
          if (mPersonality == PERSONALITY_RADIO_BUTTON) {
              setChecked(true);
          } else if (mPersonality == PERSONALITY_CHECK_BOX) {
              toggle();
          }
          return super.performClick();
      }
  
      public boolean isChecked() {
          return mChecked;
      }
  
      /**
       * &lt;p&gt;
       * Changes the checked state of this button.
       * &lt;/p&gt;
       *
       * @param checked
       *            true to check the button, false to uncheck it
       */
      public void setChecked(boolean checked) {
          if (mChecked != checked) {
              mChecked = checked;
              refreshDrawableState();
  
              // Avoid infinite recursions if setChecked() is called from a listener
              if (mBroadcasting) {
                  return;
              }
  
              mBroadcasting = true;
              if (mOnCheckedChangeListener != null) {
                  mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
              }
  
              mBroadcasting = false;
          }
      }
  
      /**
       * Register a callback to be invoked when the checked state of this button changes.
       *
       * @param listener
       *            the callback to call on checked state change
       */
      public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
          mOnCheckedChangeListener = listener;
      }
  
      /**
       * Interface definition for a callback.
       */
      public static interface OnCheckedChangeListener {
          /**
           * Called when the checked state of a button has changed.
           *
           * @param button
           *            The button view whose state has changed.
           * @param isChecked
           *            The new checked state of button.
           */
          void onCheckedChanged(CheckableImageButton button, boolean isChecked);
      }
  
      @Override
      public int[] onCreateDrawableState(int extraSpace) {
          final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
          if (isChecked()) {
              mergeDrawableStates(drawableState, CHECKED_STATE_SET);
          }
          return drawableState;
      }
  
      @Override
      protected void drawableStateChanged() {
          super.drawableStateChanged();
          invalidate();
      }
  
      static class SavedState extends BaseSavedState {
          boolean checked;
  
          SavedState(Parcelable superState) {
              super(superState);
          }
  
          private SavedState(Parcel in) {
              super(in);
              checked = (Boolean) in.readValue(null);
          }
  
          @Override
          public void writeToParcel(Parcel out, int flags) {
              super.writeToParcel(out, flags);
              out.writeValue(checked);
          }
  
          public static final Parcelable.Creator&lt;SavedState&gt; CREATOR = new Parcelable.Creator&lt;SavedState&gt;() {
              public SavedState createFromParcel(Parcel in) {
                  return new SavedState(in);
              }
  
              public SavedState[] newArray(int size) {
                  return new SavedState[size];
              }
          };
      }
  
      @Override
      public Parcelable onSaveInstanceState() {
          Parcelable superState = super.onSaveInstanceState();
          SavedState ss = new SavedState(superState);
          ss.checked = isChecked();
          return ss;
      }
  
      @Override
      public void onRestoreInstanceState(Parcelable state) {
          SavedState ss = (SavedState) state;
  
          super.onRestoreInstanceState(ss.getSuperState());
          setChecked(ss.checked);
          requestLayout();
      }
  }
  </code></pre>
  
  <p><code>checkable_image_selector</code> file content:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle"&gt;
      &lt;corners
          android:radius="4dp" /&gt;
      &lt;padding
          android:left="4dp"
          android:right="4dp"
          android:top="4dp"
          android:bottom="4dp" /&gt;
      &lt;gradient
          android:angle="90"
          android:startColor="#FFFFC700"
          android:centerColor="#FFFFA600"
          android:endColor="#FFFFC700" /&gt;
      &lt;stroke
          android:width="3dp"
          android:color="#FF80B0E0" /&gt;
  &lt;/shape&gt;
  </code></pre>
  
  <p>and <code>checkable_image_button_state_pressed</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle"&gt;
      &lt;corners
          android:radius="4dp"/&gt;
      &lt;padding
          android:bottom="4dp"
          android:left="4dp"
          android:right="4dp"
          android:top="4dp"/&gt;
      &lt;solid
          android:color="#00000000"/&gt;
      &lt;stroke
          android:width="3dp"
          android:color="#FF80B0E0"/&gt;
  &lt;/shape&gt;
  </code></pre>
  
  <p>and then i use that like with this code:</p>
  
  <pre><code>checkable_image_1.setOnCheckedChangeListener(new CheckableImageButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CheckableImageButton button, boolean isChecked) {
          Log.e("CHECK: ", isChecked + "");
          checkable_image_1.setChecked(false);
      }
  });
  checkable_image_1.setChecked(true);
  </code></pre>