java - Changing color of a button in an array of buttons and return the other buttons to default -
@override public void onclick(view v) { switch(v.getid()) { case r.id.btna: runonuithread(new thread(new runnable() { @override public void run() { btna.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.button_pressed)); btnb.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnc.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnd.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); } })); break; case r.id.btnb: runonuithread(new thread(new runnable() { @override public void run() { btnb.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.button_pressed)); btna.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnc.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnd.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); } })); break; }
so implementing code job fine, have 6 buttons have 6 times. i've read array of buttons , tried implement not make work. isn't clear me how know button clicked , changed color while other button not clicked goes default color.
edit:
sorry if wasn't clear, buttons used multiple choices. buttons set default on creation. not using switch statement make 2 buttons same color if click on button after other, same color. it's more of display problem..
i guess fredk meant that:
@override public void onclick(view v) { runonuithread(new thread(new runnable() { @override public void run() { // reset buttons btna.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnb.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnc.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); btnd.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); . . . btnz.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); // set clicked button v.setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.button_pressed)); } })); }
update:
you can iterate on viewgroup won't need write down each button manually.
viewgroup viewgroup = (viewgroup) v.getparent(); for(int i=0;i<viewgroup.getchildcount();i++){ object child = viewgroup.getchildat(i); if(child instanceof button){ ((button) child).setbackgroundcolor(contextcompat.getcolor(getapplicationcontext(), r.color.colorprimary)); } }
Comments
Post a Comment