How to arrange radio buttons in certain way

Ask by : Snake November 05, 2012 19:31

If I have a radiogroup with 7 radiobuttons and I want them to be aligned like that:

           button2    button5
button1    button3    button6
           button4    button7

Is there a way I can do the above with one RadioGroup (I am ok with even button1 being on first line but this is the general layout I am looking for)

Please help


View original question

Answer by : SimonNovember 05, 2012 19:31

You could implement your own button handling logic, based on this

public class RadioButtonHandler implements RadioButton.OnCheckedChangeListener {

    private CompoundButton currentlyCheckedButton = null;

    public void onCheckedChanged(CompoundButton button, boolean isChecked) {
            if (isChecked) {
               currentlyCheckedButton .setChecked(false);
               currentlyCheckedButton = button;

    public void addButton(RadioButton button) {
           // if this is the first button, set it as the current button
           if (currentlyCheckedButton == null) {
               currentlyCheckedButton = button;

    public CompoundButton getCheckedRadioButton() {
        return currentlyCheckedButton ;

Instantiate this class and call addButton for each button that the class should control. Then you can layout your buttons in whatever view group you need. Add extra logic to the onCheckChanged callback or call getCheckedRadioButton which will return a reference to the currently checked button.

[EDIT] Fixed typo = getCheckedButton -> getCheckedRadioButton

View original answer