Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android onCheckedChanged for radiogroup

I'm writing an Activity in android where I have two radio buttons under a RadioGroup. One of them is checked by default. But I can't trigger the event in onCreate method so that I can do something in that. The onCheckedChanged is running fine when clicked on.

RadioGroup ItemtypeGroup = (RadioGroup) findViewById(R.id.rechargeItemtype);     RadioButton providerRadio = (RadioButton) findViewById(R.id.a);     providerRadio.performClick();      ItemtypeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {          @Override         public void onCheckedChanged (RadioGroup group,int checkedId){              Log.d("chk", "id" + checkedId);              if (checkedId == R.id.a) {                 //some code             } else if (checkedId == R.id.b) {                 //some code             }         }     }); 
like image 994
user2534310 Avatar asked Aug 30 '13 15:08

user2534310


1 Answers

To fire a radio check box for the default when initializing. Set everything to unchecked with the clearCheck method, then set the handler, then set the default and your handler will fire.

itemtypeGroup.clearCheck(); 

then same as usual add a listener...

itemtypeGroup         .setOnCheckedChangeListener(new OnCheckedChangeListener() {              @Override             public void onCheckedChanged(RadioGroup group, int checkedId) { Log.d("chk", "id" + checkedId);                  if (checkedId == R.id.a) {                     //some code                 } else if (checkedId == R.id.b) {                     //some code                 }              }          }); 

Then check the default radio button and your listener will fire.

rb = (RadioButton) view.findViewById(R.id.a); rb.setChecked(true); 

Good Luck

like image 142
danny117 Avatar answered Sep 29 '22 18:09

danny117