android - set Custom Programatically created drawable to radioButton for states -
i wanted set programatically created drawable radio button checked , unchecked states, not working code follows,
code draw 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 set programatically created view(squareview) set stated 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) { } }
after little work around, realized issue drawable not have size. i'm not sure size should give adding following line makes radiobutton
visible:
shape.setsize(50, 50);
i suggest put appropriate size in dimens.xml
, use instead:
int size = context.getresources().getdimensionpixelsize(r.dimen.radio_button_size); shape.setsize(size, size);
Comments
Post a Comment