0

Is it possible to identify for which Radio Group a specific Radio Button belongs to in Android?

Eg:

RadioGroup_A
     - RadioButton_1A
     - RadioButton_2A

RadioGroup_B
     - RadioButton_1B
     - RadioButton_2B

When I have RadioButton_1A, to idetify that belongs to RadioGroup_A ?

I need to implement a logic to ignore triggering CheckedChange listener for a speacial case. Need to restrict that only for a one RadioGroup When I know that RadioButton belongs to that specific RadioGroup.

Thanks

JibW
  • 4,538
  • 17
  • 66
  • 101
  • Please explain more the case so we can understand what you want to achieve. If RadioButton_1A has unique id then its clear it will always belong to RadioGroup_A. (not getting your problem) – Ramit Aug 19 '16 at 10:39
  • Hi Ramit, Yes All the RadioGroups and RadioButtons have IDs (Eg: android:id="@+id/RadioGroup_A" / android:id="@+id/RadioButton_1A") – JibW Aug 19 '16 at 12:43
  • So if you have unique id, you want to identify radio group based on radio button at somewhere may be on checked change on onclick of radio button. So you can directly use RadioGroup_A for RadioButton_1A and RadioButton_2A. I am trying to explain with information known to me. Other wise tell exactly where you want to perform such action, sample code preferred. – Ramit Aug 19 '16 at 12:49

3 Answers3

1

What about radioGroup.indexOfChild method? The doc says that returns:

a positive integer representing the position of the view in the group, or -1 if the view does not exist in the group

Maybe you can check if your RadioButton exits in the RadioGroup:

int idx = radioGroup.indexOfChild(radioButton);

if (idx != -1)
{
   //radioButton is in the radioGroup
}
else {
   //radioButton isnt in the radioGroup
}

Hope it helps!

jos
  • 1,070
  • 12
  • 22
1

In one of my apps, I did this by having two RadioButon ArrayLists, one for each group. In onCreate() after the layout is inflated, the RadioButtons are added to each group as described in this answer.

Then, by checking for which ArrayList contains the RadioButton, the required logic can be implemented.

Community
  • 1
  • 1
Mohan Noone
  • 561
  • 6
  • 14
1

i described full example of your question please apply my solution

public class MyClass extends Activity implements RadioGroup.OnCheckedChangeListener
{
    RadioGroup rg1,rg2,rg3;
    public void onCreate(Bundle b)
    {
        super.onCreate(b);
        setContentView(R.layout.main);


        rg1=(RadioGroup)findViewById(R.id.rg1);
        rg2=(RadioGroup)findViewById(R.id.rg2);
        rg3=(RadioGroup)findViewById(R.id.rg3);


        rg1.setOnCheckedChangeListener(this);
        rg2.setOnCheckedChangeListener(this);
        rg3.setOnCheckedChangeListener(this);

    }

    @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {

        if(radioGroup==rg1)
        {
            //First RadioGroup Clicked
            RadioButton radio=(RadioButton)findViewById(i);
            Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
        }

        if(radioGroup==rg2)
        {
            //Second RadioGroup Clicked
            RadioButton radio=(RadioButton)findViewById(i);
            Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
        }

        if(radioGroup==rg3)
        {
            //Third RadioGroup Clicked
            RadioButton radio=(RadioButton)findViewById(i);
            Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
        }
        }
}
Riyaz Parasara
  • 154
  • 8
  • 20