androidradio-buttonsegmentedcontrol

Image no longer drawing on CustomRadioImageButton with Android 7


I have been using this segmented controller library for a long long time, but under Android 7, the images are no longer drawing on the radio buttons... Does anyone have any ideas what changed in this version of Android that caused this to stop working?

The segmentedcontrollerradiobutton draws, but the image that should be on the buttons no longer is drawn.

Library in use:

https://github.com/vinc3m1/android-segmentedradiobutton

package com.makeramen.segmented;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton;

public class CenteredRadioImageButton extends RadioButton {

Drawable image;

public CenteredRadioImageButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, com.makeramen.segmented.R.styleable.CompoundButton, 0, 0);
    image = a.getDrawable(1);
    setButtonDrawable(android.R.color.transparent);
    a.recycle();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (image != null) {
        image.setState(getDrawableState());

        // scale image to fit inside button

        int imgHeight = image.getIntrinsicHeight();
        int imgWidth = image.getIntrinsicWidth();
        int btnWidth = getWidth();
        int btnHeight = getHeight();
        float scale;

        if (imgWidth <= btnWidth && imgHeight <= btnHeight) {
            scale = 1.0f;
        } else {
            scale = Math.min((float) btnWidth / (float) imgWidth,
                    (float) btnHeight / (float) imgHeight);
        }


        int dx = (int) ((btnWidth - imgWidth * scale) * 0.5f + 0.5f);
        int dy = (int) ((btnHeight - imgHeight * scale) * 0.5f + 0.5f);

        image.setBounds(dx, dy, (int)(dx + imgWidth * scale), (int)(dy + imgHeight * scale));

        image.draw(canvas);
    }
}
}

Effectively the IMAGE value is always NULL in Android 7 at runtime.

 <com.makeramen.segmented.CenteredRadioImageButton
                    android:id="@+id/myButton"
                    android:layout_width="40dip"
                    android:layout_height="50dip"
                    android:button="@drawable/myImage"
                    android:clickable="true"
                    android:minWidth="40dip"
                    android:minHeight="33dip"
                    android:focusable="true">
                    </com.makeramen.segmented.CenteredRadioImageButton>

Solution

  • Figured it out, something changed in 7... the index in the TypedArray changed from being 1 to 0... modifying that solved the issue.