RadioGroup extending RelativeLayout?
Asked Answered
G

3

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
Gare answered 12/6, 2011 at 4:27 Comment(1)
[Try processing the RadioButtons without the use of RadioGroup.][1] [1]: #6332542Kickoff
R
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.

Realist answered 12/6, 2011 at 6:23 Comment(10)
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?Gare
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.Realist
Ok I did what you said, I have some errors in the new class file though, added them to the question above...Gare
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.Realist
I added all these fixes to the answer.Realist
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.Athelstan
Updated the answer. Hope it will solve your problem.Realist
Thanks @Michael, I have missed that at RadioGroup(Context context, AttributeSet attrs).Athelstan
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.Breed
Note: After getting this working, I added a project with the changes to github: github.com/valheru7/RelativeRadioGroupBreed
P
2

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

Parameter answered 12/6, 2011 at 6:11 Comment(0)
S
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

Strenuous answered 20/5, 2020 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.