20

I'm using radio group but RadioGroup onClick event is not firing and the method getCheckedRadiobuttonID() is returning null. Here is the element in my layout:

   <RadioGroup
            android:id="@+id/RG1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:onClick="onRGClick" >

Method

public void onRGClick(View v) {
    Toast.makeText(this, "Test ", 1000).show();
}
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95

4 Answers4

40

This problem can be solved using the following

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch(checkedId)
                {
                case R.id.radio0:
                    // TODO Something
                    break;
                case R.id.radio1:
                    // TODO Something
                    break;
                case R.id.radio2:
                    // TODO Something
                    break;
                }
            }
        });

Alternatively

You can use custom ids rather than default one

  RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
            rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId)
                {
                    switch(checkedId)
                    {
                    case R.id.male:
                        // TODO Something
                        break;
                    case R.id.female:
                        // TODO Something
                        break;
                    case R.id.other:
                        // TODO Something
                        break;
                    }
                }
            });
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • I was confused about this at first: The code in this answer needs to be in `OnCreate` or `OnCreateView` (but correct me if I'm wrong), I tried to just put it in my activity class initially – James Vickery Aug 15 '16 at 20:36
7

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.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
2

The other way is to assign your method to onClick property of each RadioButton. Then check for the view id. For instance,

public void onRGClick(View v) {
    int id = view.getId();
    if (id == R.id.radioButtonId) {
        //Do something
    }
}
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
Ahamed
  • 39,245
  • 13
  • 40
  • 68
2

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.

    <RadioGroup  android:onClick="onRGClick" ndroid:text="RadioButton" 
    android:id="@+id/radioGroup1" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <RadioButton android:text="RadioButton" android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content"></RadioButton>
</RadioGroup>

And here is the Activity:

public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

     }
    public void onRGClick(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.

Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
Akhil
  • 13,888
  • 7
  • 35
  • 39
  • 2
    Have you check first check and then answer it isn't working Call event for radiobutton works well but the call event for radiogroup is not working – Trikaldarshiii Mar 17 '12 at 08:40
  • I first changed your code and then made it work.Should have written the first line properly, sorry about that. did u try the code given in my answer ? – Akhil Mar 17 '12 at 10:53