Add margin between a RadioButton and its label in Android?

AndroidRadio Button

Android Problem Overview


Is it possible to add a little bit of space between a RadioButton and the label while still using Android's built-in components? By default the text looks a little scrunched.

<RadioButton android:id="@+id/rb1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="My Text"/>

I've tried a couple of things:

  1. Specifying margin and padding seem to add space around the entire element (button and text, together). That makes sense, but doesn't do what I need.

  2. Creating a custom drawable via XML specifying images for the checked and unchecked states, then adding a few extra pixels to the right side of each image. This should work, but now you are stepping outside the default UI. (Not the end of the world, but not ideal)

  3. Add extra whitespace to the beginning of each label. Android seems to trim a leading space character, as in " My String", but specifying unicode U+00A0, as in "\u00A0My String" does the trick. This works, but it seems kinda dirty.

Any better solutions?

Android Solutions


Solution 1 - Android

For anyone reading this now, the accepted answer will lead to some layout problems on newer APIs causing too much padding.

On API <= 16 you can set paddingLeft on the radio button to set the padding relative to the radio button's view bounds. Additionally, a patch nine background also changes the view bounds relative to the view.

On API >= 17 the paddingLeft (or paddingStart) is in relation to the radio button drawable. Same applies to the about a patch nine. To better illustrate padding differences see the attached screenshot.

If you dig through the code you will find a new method in API 17 called getHorizontalOffsetForDrawables. This method is called when calculating the left padding for a radio button(hence the additional space illustrated in the picture).

TL;DR Just use paddingLeft if your minSdkVersion is >= 17. If you support API <= 16, you should have radio button style for the min SDK you are supporting and another style for API 17+.

combine screenshots showing left padding differences between API versions

Solution 2 - Android

Not sure if this will fix your problem, but have you tried Radio Button's "Padding left" property with a value of 50dip or more

Solution 3 - Android

i tried several ways and finished with this one working correctly on both emulator and devices:

    <RadioButton
        android:background="@android:color/transparent"
        android:button="@null"
        android:drawableLeft="@drawable/your_own_selector"
        android:drawablePadding="@dimen/your_spacing" />
  • android:background needs to be transparent as it seems on api10 there is a background with intrinsic paddings (not tried with other apis but the solution works on others too)
  • android:button needs to be null as paddings will not work correctly otherwise
  • android:drawableLeft needs to be specified instead of android:button
  • android:drawablePadding is the space that will be between your drawable and your text

Solution 4 - Android

Add margin between a radiobutton its label by paddingLeft:

android:paddingLeft="10dip"

Just set your custom padding.

RadioButton's xml property.

<RadioButton
    android:id="@+id/radHighest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/YourImageResource"
    android:drawablePadding="50dp"
    android:paddingLeft="10dip"
    android:text="@string/txt_my_text"
    android:textSize="12sp" />

Done

Solution 5 - Android

Use the following XML attributes. It worked for me

For API <= 16 use

android:paddingLeft="16dp"

For API >= 17 use

android:paddingStart="@16dp"

Eg:

<android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/popularityRadioButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:paddingEnd="@dimen/radio_button_text"
        android:paddingLeft="@dimen/radio_button_text"
        android:paddingRight="@dimen/radio_button_text"
        android:paddingStart="@dimen/radio_button_text"
        android:text="Popularity"
        android:textSize="@dimen/sort_dialog_text_size"
        android:theme="@style/AppTheme.RadioButton" />

enter image description here

Further More: drawablePadding attribute doesn't work. It only works if you added a drawable in your radio button. For Eg:

<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:button="@null"
    android:drawableEnd="@android:drawable/btn_radio"
    android:drawablePadding="56dp"
    android:drawableRight="@android:drawable/btn_radio"
    android:text="New RadioButton" />

Solution 6 - Android

Can't try this right now to verify, but have you tried to see if the attribute android:drawablePadding does what you need?

Solution 7 - Android

final float scale = this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),

    checkBox.getPaddingTop(),
    checkBox.getPaddingRight(),
    checkBox.getPaddingBottom());

Solution 8 - Android

The padding between the drawables and the text. It will be achieved by adding line below in xml file. android:drawablePadding="@dimen/10dp"

Solution 9 - Android

jusst use padding start for API above 16

Solution 10 - Android

I have tried, "android:paddingLeft" will works. paddingLeft will only impact the text while keep the radio image stay at the original position.

Solution 11 - Android

The "android:paddingLeft" only seems to work correctly under android 4.2.2

i have tried almost all versions of android and it only works on the 4.2.2 version.

Solution 12 - Android

I'm using different approach that I think should work on all API versions. Instead of applying padding I'm adding an empty view between to RadioButtons:

<View
        android:layout_width="20dp"
        android:layout_height="1dp" />

This should give you 20dp padding.

Solution 13 - Android

I came here looking for an answer and the simplest way (after some thinking) was add spacing at the beginning of the label itself like so

<RadioGroup
     android:orientation="horizontal"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignRight="@+id/btnChangeMode"
     android:layout_marginTop="10dp"
     android:layout_marginBottom="10dp"
     android:layout_below="@+id/view3"
     android:gravity="center|left"
     android:id="@+id/ledRadioGroup">
<RadioButton
         android:button="@drawable/custom_radio_button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text=" On"
         android:layout_marginRight="6dp"
         android:id="@+id/ledon"
         android:textColor="@color/white" />
<RadioButton
         android:button="@drawable/custom_radio_button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text=" Off"
         android:layout_marginLeft="6dp"
         android:id="@+id/ledoff"
         android:textColor="@color/white" />

Solution 14 - Android

for me works:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <layer-list>
                <item android:right="5dp">
                    <shape android:paddingLeft="5dp" android:shape="oval">
                     <size android:width="20dp" android:height="20dp" />
                     <solid android:color="@color/blue" />
            </shape>
        </item>
    </layer-list>
</item>
<item android:paddingLeft="5dp" android:state_checked="false">
    <layer-list>
        <item android:right="5dp">
            <shape android:paddingLeft="5dp" android:shape="oval">
                <size android:width="20dp" android:height="20dp" />
                <solid android:color="@color/grey" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

Solution 15 - Android

<RadioButton
                android:id="@+id/rb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="@null"
                android:paddingLeft="20dp"
                android:text="1"
                android:textColor="@color/text2"
                android:textSize="16sp"
                android:textStyle="bold" />

Solution 16 - Android

You could try using the gravity attribute of radio button .

<RadioButton
                        android:id="@+id/rb_all"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:gravity="right|center_vertical"
                        android:padding="@dimen/padding_30dp"
                        android:text="@string/filter_inv_all"
                        android:textColor="@color/black"
                        android:textSize="@dimen/text_size_18" />

This will align the text to the right most end . Check out the first radio in the image.

enter image description here

Solution 17 - Android

You can this code on your XML file

<RadioButton
android:id="@+id/rButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="@string/your_text" />

or use this on Activity class

radioButton.setPadding(12, 10, 0, 10);

Solution 18 - Android

Create a style in style.xml like this

<style name="Button.Radio">

    <item name="android:paddingLeft">@dimen/spacing_large</item>
    <item name="android:textSize">16sp</item>
</style>

Put that style in radio button

 <RadioButton
                android:id="@+id/rb_guest_busy"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="@string/guest_is_waiting"
                android:textSize="@dimen/font_size_3x_medium"
                android:drawablePadding="@dimen/spacing_large"
                android:textColor="@color/color_text_heading_dark"
                style="@style/Button.Radio"/>

You can change any attribute same as button as it RadioButton indirectly inherits button.

Solution 19 - Android

I know it is an old question, but with this solution, I finally got peace of mind and forget about API level.

Left side vertical RadioGroup with right side vertical text view.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:gravity="center_vertical"
            android:text="Radio 1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:gravity="center_vertical"
            android:text="Radio 2"/>
    </LinearLayout>
</LinearLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionallclawsView Question on Stackoverflow
Solution 1 - AndroidJim BacaView Answer on Stackoverflow
Solution 2 - Androiduser438650View Answer on Stackoverflow
Solution 3 - AndroidGianluca P.View Answer on Stackoverflow
Solution 4 - AndroidHiren PatelView Answer on Stackoverflow
Solution 5 - AndroidSrikar ReddyView Answer on Stackoverflow
Solution 6 - AndroidMark BView Answer on Stackoverflow
Solution 7 - AndroidDilavar MalekView Answer on Stackoverflow
Solution 8 - AndroidFaakhirView Answer on Stackoverflow
Solution 9 - AndroidToseef AhmedView Answer on Stackoverflow
Solution 10 - AndroidDavid GuoView Answer on Stackoverflow
Solution 11 - AndroidLowerlandView Answer on Stackoverflow
Solution 12 - AndroidOren BengigiView Answer on Stackoverflow
Solution 13 - AndroidjtizzleView Answer on Stackoverflow
Solution 14 - AndroidVadimView Answer on Stackoverflow
Solution 15 - AndroidDhaval ShingalaView Answer on Stackoverflow
Solution 16 - AndroidSalil KaulView Answer on Stackoverflow
Solution 17 - AndroidFakhriddin AbdullaevView Answer on Stackoverflow
Solution 18 - AndroidVindhya Pratap SinghView Answer on Stackoverflow
Solution 19 - AndroidStanley KoView Answer on Stackoverflow