4

The layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/MyLL"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal" >

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

            <RadioButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:gravity="center" />

            <RadioButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:gravity="center" />

    </RadioGroup>

</LinearLayout>

results in the two RadioButtons splitting the horizontal space equally.

That is good, but rather than having them flush to the left, I would like them to be centered. Why is android:gravity="center" not giving this effect?

I have tried this question earlier with CheckBoxes, but the replies suggested inserting a gratuitous LinearLayout for each View (CheckBox). I can't argue against an answer that is neither elegant nor efficient—as long as it works.

But that trick does not work with RadioButtons. The RadioGroup must be the direct parent of a collection of RadioButtons for them to uncheck each other when clicked.

Edit

With the XML declarations above I am getting the first of the sketches below. I am not looking for any of the first three, but for the fourth.

enter image description here

Community
  • 1
  • 1
Calaf
  • 10,113
  • 15
  • 57
  • 120

2 Answers2

1

Okay, so sad news, the RadioButton class is actually hard-coded that the radio drawable will be aligned to the left. The only solution I've found is to subclass RadioButton and handle positioning a Drawable on your own.

Here's an alternate solution (without using RadioGroup at all)

Community
  • 1
  • 1
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • I see. That's very interesting, but what about centering the simulated RadioButtons or ordinary CheckBoxes? Must one throw in an extra LinearLayout for each widget to center it? – Calaf May 22 '13 at 21:42
  • Nope, you could just use a TextView with compound drawable and give the TextView a center gravity. – Kevin Coppock May 22 '13 at 21:59
  • So the problem is that a RadioButton consists internally of a LinearLayout parent of the drawable and the text, but that the drawable is hard-wired to be on the left of the (internal) LinearLayout. If that is the case, might there be a way to access the internal drawable and set its gravity? – Calaf May 23 '13 at 08:21
  • Possibly using reflection, but that's not really safe to use in cases like this, as the internal structure of the RadioButton might change between OS versions. – Kevin Coppock May 23 '13 at 16:42
0
<style name="TextlessRadioButton" parent="android:Widget.Material.CompoundButton.RadioButton">
    <item name="android:button">@null</item>
    <item name="android:foreground">?android:listChoiceIndicatorSingle</item>
    <item name="android:foregroundGravity">center</item>
</style>
j__m
  • 9,392
  • 1
  • 32
  • 56