Text leftside of a RadioButton with a margin on Android

23,816

You can achieve this by setting android:button="@null" and adding the drawable of the RadioButton as android:drawableRight. You can change the Padding between the text and the drawable with android:drawablePadding .

    <RadioGroup
        android:id="@+id/radios"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:inputType="text"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/first"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:drawablePadding="20dp"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="@string/first"
            android:textSize="20dip" />

        <RadioButton
            android:id="@+id/second"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:drawablePadding="20dp"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="@string/second"
            android:textSize="20dip" />
    </RadioGroup>
</RelativeLayout>
Share:
23,816
Yalla T.
Author by

Yalla T.

Yalla Yalla

Updated on February 18, 2021

Comments

  • Yalla T.
    Yalla T. about 3 years

    I want to have the "label", - the text you set in your RadioButton to show left of the button and have some padding in between.

    Adding an additional TextView into the layout doesn't work because my RadioGroup does not work (i can choose multiple buttons) if i add anything other then a RadioButton into the RadioGroup.

    So, how can i change the RadioButton to be <text><buttondrawable> instead of <buttondrawable><text>

  • Yalla T.
    Yalla T. over 11 years
    Having two of those inside of a RadioGroup, I can still select both.
  • Yalla T.
    Yalla T. over 11 years
    Thanks! Very clever solution and i can easily add the padding i wanted with android:drawablePadding.
  • Yalla T.
    Yalla T. over 11 years
    The TextView still reserves space for the Button on the left, but when you set a negative margin/padding on the left, you can clean that up.
  • Yalla T.
    Yalla T. about 11 years
    regarding my previous comment, the space is not reserved on JellyBean So you have to make another Version without the negative Padding for JellyBean Devices.
  • Jarett Millard
    Jarett Millard over 10 years
    Setting android:background="@null" on the RadioButton will remove the mysterious padding.
  • Jeroen Kransen
    Jeroen Kransen almost 10 years
    Please add the essence in a code snippet, future readers should not have to follow your link, unless they want more in-depth information.
  • Jeroen Kransen
    Jeroen Kransen almost 10 years
    Also, the order of answers may vary, so better refer to an answer by the poster's name.