Add Margin Between a Radiobutton and Its Label in Android

Text leftside of a RadioButton with a margin on Android

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>

How to provide spacing between android Radio Buttons of Vertical orientation

Try using the layout_margin attribute, it works similarly to padding, but may have the effect you want. You can use simply android:layout_margin="20dp" or you can set individual sides, like you can for padding (ex: android:layout_marginLeft="20dp").

Add padding to the left of radio button drawable

RadioButton is a subclass of CompundButton, so we get drawable on the compound button and add inset to it.

val compoundButtonDrawable = CompoundButtonCompat.getButtonDrawable(radioButton1)
val insetDrawable = InsetDrawable(compoundButtonDrawable, 32, 0, 0, 0)
radioButton1.buttonDrawable = insetDrawable

Android - Spacing between CheckBox and text

I hate to answer my own question, but in this case I think I need to. After checking it out, @Falmarri was on the right track with his answer. The problem is that Android's CheckBox control already uses the android:paddingLeft property to get the text where it is.

The red line shows the paddingLeft offset value of the entire CheckBox

alt text

If I just override that padding in my XML layout, it messes up the layout. Here's what setting paddingLeft="0" does:

alt text

Turns out you can't fix this in XML. You have do it in code. Here's my snippet with a hardcoded padding increase of 10dp.

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

This gives you the following, where the green line is the increase in padding. This is safer than hardcoding a value, since different devices could use different drawables for the checkbox.

alt text

UPDATE - As people have recently mentioned in answers below, this behavior has apparently changed in Jelly Bean (4.2). Your app will need to check which version its running on, and use the appropriate method.

For 4.3+ it is simply setting padding_left. See htafoya's answer for details.

Android: RadioButton with two lines does not keep padding/margin

My fault, since when setting the name of the category programmatically, it had some blank spaces before the name, hence the "aggregated" padding to the left of the RadioButton in the first line.
Thanks everybody for the replies!



Related Topics



Leave a reply



Submit