I have a custom ImageButton class mimicing the ToggleButton's checked state based on this tutorial How to add a custom button state .
Everything works fine, when I have a state list drawable as the android:src attribute, but the custom state doesn't work with the ImageButton's android:background attribute.
Here is my code:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageButton;
public class CheckableImageButton extends ImageButton implements Checkable {
    private static final int[] STATE_CHECKED = {R.attr.state_checked};
    private boolean mChecked = false;
    public CheckableImageButton(Context context) {
        super(context);
    }
    public CheckableImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if(mChecked){
            mergeDrawableStates(drawableState, STATE_CHECKED);
        }
        return drawableState;
    }
    @Override
    public boolean isChecked() {
        return mChecked;
    }
    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        refreshDrawableState();
    }
    @Override
    public void toggle() {
        setChecked(!mChecked);
    }
}
And the relevant snippet from the layout XML:
<com.my.package.view.CheckableImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@drawable/header_button_bg"
    android:padding="5dp"
    android:src="@drawable/menu_button"
    tools:ignore="ContentDescription" />
And the state list drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/my.package" >
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />
            <gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item app:state_checked="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />
            <gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />
            <gradient android:angle="-90" android:endColor="#4f5b6c" android:startColor="#345b75" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>
 
    