7

I'm trying to make a grid of radio buttons for my app, what I have learned is that this isn't possible using regular RadioGroup because it extends LinearLayout and if you try to arrange the RadioButtons using RelativeLayout INSIDE the RadioGroup the RadioGroup doesn't see the Buttons inside the RelativeLayout.

So in order to fix this I want to make a custom RadioGroup that extends RelativeLayout instead of LinearLayout.

How do I do this?

UPDATE: I did what you said but I have these errors I don't know how to fix in the class file:

Description Resource    Path    Location    Type
RadioGroup_checkedButton cannot be resolved or is not a field   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 81 Java Problem
The constructor RelativeLayout.LayoutParams(int, int, float) is undefined   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 265    Java Problem
The method setOnCheckedChangeWidgetListener(CompoundButton.OnCheckedChangeListener) is undefined for the type RadioButton   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 363    Java Problem
The method setOnCheckedChangeWidgetListener(null) is undefined for the type RadioButton RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 377    Java Problem
VERTICAL cannot be resolved to a variable   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 68 Java Problem
Widget_CompountButton_RadioButton cannot be resolved or is not a field  RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 79 Java Problem
Kyle V.
  • 4,752
  • 9
  • 47
  • 81
  • [Try processing the RadioButtons without the use of RadioGroup.][1] [1]: http://stackoverflow.com/questions/6332042/setting-up-a-radiogroup-programmatically/7019673#7019673 – Matt Aug 11 '11 at 01:00

3 Answers3

9

You need to get the RadioGroup's source code from here, replace all entries of LinearLayout with RelativeLayout.

Add this code to some xml file in your project (usually its name is attrs.xml):

<resources>
    <declare-styleable name="RadioGroup">
        <attr name="android:checkedButton" />
    </declare-styleable>
</resources>

Replace RadioGroup's constructors with these:

public RadioGroup(Context context) {
    super(context);
    if (!isInEditMode()) {
        init();
    }
}

public RadioGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (!isInEditMode()) {
        TypedArray attributes = context.obtainStyledAttributes(
                attrs, R.styleable.RadioGroup, 0,
                android.R.style.Widget_CompoundButton_RadioButton);

        int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton,
            View.NO_ID);
        if (value != View.NO_ID) {
            mCheckedId = value;
        }

        attributes.recycle();
        init();
    }
}

Remove the following constructor from the LayoutParams inner class:

public LayoutParams(int w, int h, float initWeight) {
    super(w, h, initWeight);
}

Replace all occurrences of setOnCheckedChangeWidgetListener() method calls with the setOnCheckedChangeListener() method. IMPORTANT: In this case it won't be possible to use this method from a code that uses this widget.

Haven't tried this but hope this will work.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • This sounds good, but what is the XML and the changes to the constructor for? I was thinking just repalce LinearLayout with RelativeLayout and that would be enough, am I wrong? – Kyle V. Jun 12 '11 at 06:30
  • Yes, you're wrong. It won't just compile, because some internal fields are used in `RadioGroup'`s constructor. That's why you need this xml. It allows you to redefine `android:checkedButton` attribute for the `RadioGroup` widget. And changes to the constructor are necessary because you need to use values from the xml. – Michael Jun 12 '11 at 06:43
  • Ok I did what you said, I have some errors in the new class file though, added them to the question above... – Kyle V. Jun 12 '11 at 17:50
  • 1) Attrs.xml file must have resources as the root tag. Added it to the answer. 2) Just remove this constructor. 3,4) That's a problem. This method is package-private, so it's not available from your code. You can use 'setOnCheckedChangeListener', but you won't be able to use it in other parts of code in this case. 5) Remove line `setOrientation(VERTICAL)` from the constructor. 6) That was just a type in the word Compound. Fixed it in the answer. – Michael Jun 12 '11 at 18:18
  • I added all these fixes to the answer. – Michael Jun 12 '11 at 18:25
  • Hi @Michael, I'm trying to implement this but I get the following error: `Style with id 0x103001a (resolved to 'Widget_CompoundButton_RadioButton') does not exist.` At xml Graphical Layout view. Any idea? Thanks. – ramaral Dec 09 '13 at 20:10
  • Updated the answer. Hope it will solve your problem. – Michael Dec 10 '13 at 10:31
  • Thanks @Michael, I have missed that at `RadioGroup(Context context, AttributeSet attrs)`. – ramaral Dec 10 '13 at 18:57
  • I have to say thanks! I was able to get this working for me! Had an issue with R.styleable not finding the RadioGroup checkedButton in my attrs.xml but I just renamed it and it found it fine then. – valheru Jan 08 '14 at 20:30
  • 1
    Note: After getting this working, I added a project with the changes to github: https://github.com/valheru7/RelativeRadioGroup – valheru Jan 10 '14 at 04:30
2

Copy the source for RadioGroup from here and edit it to change it to extend RelativeLayout instead of LinearLayout.

Femi
  • 64,273
  • 8
  • 118
  • 148
0

This is out of topic, but in case if you wanna do the same thing with ConstraintLayout then the code is available at Github

You can import the library and use the widget ConstraintRadioGroup

smallfoxy
  • 51
  • 6