Adding a Radio Button Group to views

This one turned out to be a tough one and I had to spend quite a lot of time to figure it out and eventually got to know that there is no straight way to achieve this.

The problem was that I wanted to have a layout in which there was a text at the top, and five options for selecting the answer with radio buttons in front of each options to select one of those. Everything is straight till the time you get to the last part, i.e. selecting ONE of the options.

In android, you need to put the radio buttons in a radioGroup to enable single selection among that group. But its MANDATORY that the radiobuttons have the radio group as the immediate parent and hence if you are looking for any sort of a layout other than the one in which your radiobuttons are immediate children of a radiogroup, you have to go down the hard way.

The links this and this will give you a fairly nice idea as to how to approach this issue. In short, just create an onClickListener() and attach it to each of the buttons in your layout. In the onClick() method, de-select all other buttons and select the one which has been clicked.

 

Share Comments
comments powered by Disqus