user3057944
user3057944

Reputation: 711

RadioButton with custom Drawable

I try to set up custom drawables for RadioButtons, but I have some issues. (These drawables are just muck up, designers and I try to find out the best way to generate and use icons exported from Paint Code). These are my drawables: for the selected state

public class SelectedRadioButtonDrawable extends Drawable {
private RectF rectF;

public SelectedRadioButtonDrawable(Context context) {
    final int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, context.getResources().getDisplayMetrics());
    rectF = new RectF(0, 0, size, size);
}

@Override
public void draw(@NonNull Canvas canvas) {
    StyleKitNameJava.drawSave(canvas, rectF, StyleKitNameJava.ResizingBehavior.AspectFill, true, false);
}

@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

}

@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {

}

@Override
public int getOpacity() {
    return PixelFormat.OPAQUE;
}

and for default state:

public class RadioButtonDrawable extends Drawable {
    private RectF rectF;

    public RadioButtonDrawable(Context context) {
        final int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, context.getResources().getDisplayMetrics());
        rectF = new RectF(0, 0, size, size);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        StyleKitNameJava.drawSave(canvas, rectF, StyleKitNameJava.ResizingBehavior.Center, false, false);
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}

Here is my DrawableStateList

public class RadioButtonStateList {
private StateListDrawable stateListDrawable;

public StateListDrawable getStateListDrawable(Context context) {
    stateListDrawable = new StateListDrawable();
    Drawable radioButtonDrawable = new RadioButtonDrawable(context);
    Drawable selectedRadioButtonDrawable = new SelectedRadioButtonDrawable(context);
    stateListDrawable.addState(new int[]{android.R.attr.state_selected}, selectedRadioButtonDrawable);
    stateListDrawable.addState(StateSet.WILD_CARD, radioButtonDrawable);
    return stateListDrawable;
}
}

And I set up the radio button here

            for (VoteMonsterOption option : options) {
            optionName = option.getOption();
            optionId = option.getId();
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setPadding(padding, padding, padding, padding);
            radioButton.setButtonDrawable(new RadioButtonStateList().getStateListDrawable(getContext()));
            if (optionName != null) radioButton.setText(optionName);
            if (optionId != null) radioButton.setTag(optionId);
            voteRadioGroup.addView(radioButton);
        }

My first problem, the text of the RadioButton overlaps the Drawable, and I can not find out how to solve it. My second problem, the state list is not working. If I click one of the buttons it's drawable does not change to selected state.

Upvotes: 0

Views: 417

Answers (0)

Related Questions