Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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();
}
like image 998
Trikaldarshiii Avatar asked Mar 17 '12 07:03

Trikaldarshiii


People also ask

How check radio group is selected or not in android?

Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected. You are already doing this. So If selectedId is -1 then it means no gender is selected.

Which event is achieved when a radio button is selected?

You can use the onchange event, which will fire when the radio selection is changed (ie. the first time a radio button in the group is clicked or when the selection within the group is changed).

What appears in Circle of radio button when it is selected?

A Radio Button consists of a small circle and descriptive caption. A dot appears in the circle of the button that is selected. The absence of a dot indicates that the button is not selected. Radio Buttons often are used on forms or tests when users must make a single selection from multiple choices.


1 Answers

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;
                    }
                }
            });
like image 105
Trikaldarshiii Avatar answered Sep 17 '22 01:09

Trikaldarshiii