Reputation: 4562
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
Upvotes: 0
Views: 70
Reputation: 154
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();
}
}
}
Upvotes: 1
Reputation: 711
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.
Upvotes: 1
Reputation: 1070
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!
Upvotes: 1