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 scrunche
The padding between the drawables and the text. It will be achieved by adding line below in xml file.
android:drawablePadding="@dimen/10dp"
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+.
final float scale = this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
checkBox.getPaddingTop(),
checkBox.getPaddingRight(),
checkBox.getPaddingBottom());
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.
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>
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.