Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top