Issue
I wanted to set a programatically created drawable to radio button for its checked and unchecked states, but it is not working my code is as follows,
Code to draw a rectangular box,
public static GradientDrawable squareView(int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
//shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
return shape;
}
Code to set programatically created View(squareview) to set as stated to radiobutton,
public static void setChecked_Selector(Context context,RadioButton view) {
try {
Drawable pressed=squareView(ContextCompat.getColor(context,R.color.colorBlue),ContextCompat.getColor(context,R.color.colorRed));//new BadgeDrawable(context,colorPressed);
Drawable normal=squareView(ContextCompat.getColor(context,R.color.colorwhite),ContextCompat.getColor(context,R.color.colorRed));
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_checked,},pressed);
states.addState(new int[]{android.R.attr.state_pressed}, pressed);
states.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, pressed);
states.addState(new int[]{android.R.attr.state_checked, -android.R.attr.state_enabled}, pressed);
states.addState(new int[]{}, normal);
view.setButtonDrawable(states);
} catch (Exception e) {
}
}
Solution
After a little work around, I realized that the issue is the drawable does not have any size. I'm not sure what size you should give but just adding following line makes your RadioButton
visible:
shape.setSize(50, 50);
I would suggest to put appropriate size for it in dimens.xml
and use this instead:
int size = context.getResources().getDimensionPixelSize(R.dimen.radio_button_size);
shape.setSize(size, size);
Answered By - Rehan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.