android - Programmatically set custom drawables for radio buttons -
i'm developing basic paint application in android , can't seem programmatically set custom drawables radio buttons. these radio buttons consist of layerdrawable
white colordrawable
borders , inset yellow (or whatever color is) colordrawable
center. put layerdrawable
along 1 (it has black borders indicate selection) in statelistdrawable
preserve radiobutton
functionality.
but when try setbuttondrawable(mydrawable)
, radiobutton
occupies small region though specify width , height 30dp
.
and when try setbuttondrawable(null)
, setbackground(mydrawable)
, radiobutton
no longer has functionality.
here code
private void setupcolorbuttons() { int[] colors = {color.red, color.green, color.blue, color.yellow, color.cyan, color.magenta}; int childcount = mcolorgroup.getchildcount(); (int = 0; < childcount; i++) { radiobutton colorbutton = (radiobutton) mcolorgroup.getchildat(i); colorbutton.setbuttondrawable(createcolorbuttondrawable(colors[i])); } } private drawable createcolorbuttondrawable(int color) { colordrawable center = new colordrawable(color); colordrawable selborder = new colordrawable(r.color.black); colordrawable unselborder = new colordrawable(r.color.white); layerdrawable sel = new layerdrawable(new drawable[] {selborder, center}); sel.setlayerinset(1, 2, 2, 2, 2); layerdrawable unsel = new layerdrawable(new drawable[] {unselborder, center}); unsel.setlayerinset(1, 2, 2, 2, 2); statelistdrawable states = new statelistdrawable(); states.addstate(new int[] {android.r.attr.state_checked}, sel); states.addstate(new int[] {-android.r.attr.state_checked}, unsel); return states; }
i've looked @ similar questions, every solution requires me create custom xml styles. i'm wondering if knows how set custom statelistdrawable
programmatically. in advance help!
try code:
statelistdrawable mstate1 = new statelistdrawable(); mstate1.addstate(new int[] { android.r.attr.state_pressed }, getresources().getdrawable(r.drawable.button3_pressed)); mstate1.addstate(new int[] { android.r.attr.state_focused }, getresources().getdrawable(r.drawable.button3_focused)); mstate1.addstate(new int[] {}, getresources().getdrawable(r.drawable.button3_up)); mybutton.setbackgrounddrawable(mstate1);
Comments
Post a Comment