Android radio button uncheck -


the application step sequencer application 16 radio groups 8 buttons in each group. works except once group has button selected cant turn off unless use clear button have created clear radiogroups. add code says when selected radio button selected again turns off toggle. tried using toggles other issues arose method. below 2 attempts both stops me using button

     final radiogroup radiogroup1 = (radiogroup)findviewbyid(r.id.radiogroup1);      radiobutton d1 = (radiobutton)findviewbyid(r.id.radiobuttond1);       button d1 = (button)findviewbyid(r.id.radiobuttond1);     d1.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             pdbase.sendfloat("d1", 74);             int selectedtypeid = radiogroup1.getcheckedradiobuttonid();             radiobutton d1 = (radiobutton) findviewbyid(selectedtypeid);             if(d1 != null) // null if none of radio buttons selected                    radiogroup1.clearcheck();              pdbase.sendfloat("d1", 0);         }     });  radiobutton lc1 = (radiobutton)findviewbyid(r.id.radiobuttonlowc1);         lc1.setonclicklistener(new view.onclicklistener() {                public void onclick(view v) {                   int selectedtypeid = radiogroup1.getcheckedradiobuttonid();                  radiobutton lc1 = (radiobutton) findviewbyid(r.id.radiobuttonlowc1);                 if (selectedtypeid == -1){                 pdbase.sendfloat("lc1", 72);                 }                 else if (selectedtypeid == r.id.radiobuttonlowc1) {                        radiogroup1.clearcheck();                          pdbase.sendfloat("lc1", 0);                  }             }            }); 

i had same need - have radio group selected item deselected tapping again. found couldn't accomplish using listeners was able using custom radiobutton, so...

public class toggleableradiobutton extends radiobutton {     // implement necessary constructors      @override     public void toggle() {         if(ischecked()) {             if(getparent() instanceof radiogroup) {                 ((radiogroup)getparent()).clearcheck();             }         } else {             setchecked(true);         }     } } 

notice button toggled in different ways depending on current state - i.e., calling setchecked(true) on button vs. calling clearcheck() on group. if setchecked() used in both cases, button deselected cannot re-selected - logic in radiogroup seems deselect it.

to use button, replace <radiobutton> tags <your.package.toggleableradiobutton> in layout xml.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -