I created a RadioGroup layout in XML. So I creating it dynamically:
RadioGroup segmentRadioGroup = new RadioGroup(parentActivity);
inflater.inflate(R.layout.segm_btn_stores, segmentRadioGroup);
segmentRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
showMap();
}
});
Oh, it doesn’t work! showMap is not firing!
But… wait. What if we do it this way?
RadioGroup segmentRadioGroup = (RadioGroup) inflater.inflate(R.layout.segm_btn_stores, null);
It… works. Why? segmentRadioGroup is RadioGroup in both cases. And if I pass segmentRadioGroup created before instead of null it won’t work too.
In the above line, you create an ’empty’
RadioGroup. Then……in the above line, you inflate another
RadioGroupfrom the layout file and it is then ‘added’ to the firstRadioGroup. The logic here seems to be that asRadioGroupextends (and effectively IS)LinearLayout, it is legal for aRadioGroupto contain anotherRadioGroup.Finally, on the line above, you set the listener on the outer / parent
RadioGroupand not on the innerRadioGroup. As such, theonCheckedChanged(...)method is never called for the innerRadioGroup.Well that’s the only logic I can come up with.
With your second approach…
You are simply inflating one
RadioGroupwithout an outer parent layout because you pass ‘null’ as the second parameter.