Radio Group Onclick Event Not Firing, How Do I Tell Which Is Selected?
Solution 1:
This problem can be solved using the following
RadioGrouprg= (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(newOnCheckedChangeListener()
{
@OverridepublicvoidonCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.radio0:
// TODO Somethingbreak;
case R.id.radio1:
// TODO Somethingbreak;
case R.id.radio2:
// TODO Somethingbreak;
}
}
});
Alternatively
You can use custom ids rather than default one
RadioGrouprg= (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(newOnCheckedChangeListener()
{
@OverridepublicvoidonCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.male:
// TODO Somethingbreak;
case R.id.female:
// TODO Somethingbreak;
case R.id.other:
// TODO Somethingbreak;
}
}
});
Solution 2:
Actually I think you do not need to add android:clickable="true"
or click listener. You can declare RadioGroup.OnCheckedChangeListener
which will listen for change of the selection. Please see this thread for example how you can use that.
Solution 3:
EDIT : I did some changes in your code and its working for me. onClick works for all the views. Here in your code, Rgroup's width is wrap_content, so if you have put RadioButtons inside the RG (which will completely overlap the RG), your clicks would be consumed by the RadioButtons (and not the Rgroup). I made the Rgroup's width to fill_parent and the click was getting executed. Here is my sample so that you can try it out.
<RadioGroupandroid:onClick="onRGClick"ndroid:text="RadioButton"android:id="@+id/radioGroup1"android:layout_width="fill_parent"android:layout_height="wrap_content"><RadioButtonandroid:text="RadioButton"android:id="@+id/radioButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"></RadioButton></RadioGroup>
And here is the Activity:
publicclassHelloextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
publicvoidonRGClick(View v){
Toast.makeText(this, "Test ", Toast.LENGTH_LONG).show();
}
Just click anywhere to the right of the RadioButton, and you will see the Toast.
Although for general purposes,OnCheckedChangeListener
is more useful.
Hope this helps you.
Solution 4:
The other way is to assign your method to onClick
property of each RadioButton
. Then check for the view id. For instance,
publicvoidonRGClick(View v){
int id = view.getId();
if (id == R.id.radioButtonId) {
//Do something
}
}
Post a Comment for "Radio Group Onclick Event Not Firing, How Do I Tell Which Is Selected?"