Search code examples
androidxmlandroid-layoutandroid-radiogroupandroid-radiobutton

How can I get my Android radio buttons to use less padding?


I want my radio buttons to get a little cozier with each other.

I hoped to put them in a radiogroup, thinking that would automatically provide the "if this one is checked, the other ones automatically uncheck" logic.

But with a radiogroup:

<TableRow
        android:id="@+id/tableRow6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip">

        <RadioGroup
            android:id="@+id/radioEditList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

        <RadioButton
            android:id="@+id/radioAlways"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="@string/editlist_radgrp_always" />

        <RadioButton
            android:id="@+id/radioNever"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/editlist_radgrp_never" />

        <RadioButton
            android:id="@+id/radioCostChange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/editlist_radgrp_costchange" />
    </RadioGroup>
</TableRow>

...it looks like this:

enter image description here

Without the RadioGroup:

<TableRow
    android:id="@+id/tableRow6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip">

    <RadioButton
        android:id="@+id/radioAlways"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/editlist_radgrp_always" />

    <RadioButton
        android:id="@+id/radioNever"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/editlist_radgrp_never" />

    <RadioButton
        android:id="@+id/radioCostChange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/editlist_radgrp_costchange" />
</TableRow>

...it looks better:

enter image description here

...but still pitifully, or at least woefully, inadequate.

How do I get thee radio buttons to scrunch up together?


Solution

  • You can try putting it all inside a

    LinearLayout 
    

    and specifying weights for each button. Something like this:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:padding="5dip" >
    
        <RadioButton
            android:id="@+id/radioAlways"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="always" />
    
        <RadioButton
            android:id="@+id/radioNever"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="never" />
    
        <RadioButton
            android:id="@+id/radioCostChange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="costchange" />
    </TableRow>