How to center text into radio button
Asked Answered
D

5

10

I have customized the original radiobutton with a specific background and by removing the button :

styles.xml :

<style name="echelle_relecture">
    <item name="android:background">@drawable/btn_relecture_echelle</item>
    <item name="android:button">@null</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>

radio.xml :

<RadioGroup android:layout_height="wrap_content" android:id="@+id/radioGroup1" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_marginTop="10dp">
        <RadioButton style="@style/echelle_relecture" android:text="@string/day" android:id="@+id/jour" android:checked="true"></RadioButton>
        <RadioButton style="@style/echelle_relecture" android:text="@string/hour" android:id="@+id/hour" android:button="@null"></RadioButton>
        <RadioButton style="@style/echelle_relecture" android:text="@string/min" android:id="@+id/mins" android:button="@null" android:enabled="false"></RadioButton>
</RadioGroup>

Despite the "center_horizontal" parameter i can't get the text centered inside the button. How can i do that ?

Disappearance answered 4/8, 2011 at 11:41 Comment(0)
B
19

You should use gravity, not layout_gravity to centralize text inside RadioButton.

Berkeley answered 4/8, 2011 at 11:43 Comment(0)
L
9

use this

android:gravity="center"
Lawsuit answered 4/8, 2011 at 11:43 Comment(0)
G
2
<RadioButton
    ...
    android:textAlignment="center"
    ... />
Generalization answered 6/1, 2017 at 9:44 Comment(2)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Rheo
This code is as self explanatory as it gets, come on!Koblenz
O
0

Either setting android:paddingLeft="0dp" or android:paddingRight="0dp" should work to center the text

Outfoot answered 31/7, 2015 at 13:18 Comment(0)
G
0

add the code in your style <item name="android:gravity">center</item>

Generalization answered 31/12, 2018 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.