Radio group onclick event not firing, how do I tell which is selected?
Asked Answered
W

4

21

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();
}
Watford answered 17/3, 2012 at 7:9 Comment(0)
W
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;
                    }
                }
            });
Watford answered 7/7, 2013 at 1:27 Comment(1)
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 initiallyIvo
R
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.

Repository answered 17/3, 2012 at 7:52 Comment(2)
where need to add android:clickable="true" ?Malvin
@Sametöztoprak nowhere, as I have written you do not need this attributeRepository
B
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.

Bergamot answered 17/3, 2012 at 8:33 Comment(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 workingWatford
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 ?Bergamot
C
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
    }
}
Comical answered 27/9, 2012 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.