I have three radiobuttons and I want to evenly space them across the screen. When I use android:layout_weight="1"
, the buttons are stretched out across the screen. So how would I have the same amount of space in between each of them that also scales on different screen sizes?
<RadioGroup android:id="@+id/auton_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="10dp" android:layout_below="@id/clear_fields" > <RadioButton android:id="@+id/auton_radio_1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/auton_col" android:layout_weight="1" /> <!-- android:layout_marginRight="380dp" --> <RadioButton android:id="@+id/auton_radio_2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/auton_col" android:layout_weight="1" /> <RadioButton android:id="@+id/auton_radio_3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/auton_col" android:layout_weight="1" /> </RadioGroup>
To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup . By grouping them together, the system ensures that only one radio button can be selected at a time.
You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection. Save this answer.
Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.
RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.
If you want them to share screen width equally you need to set android:layout_width="match_parent"
on each View
. Your xml would become:
<RadioGroup android:id="@+id/auton_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/clear_fields" android:orientation="horizontal" android:paddingLeft="10dp" > <RadioButton android:id="@+id/auton_radio_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/auton_col" /> <!-- android:layout_marginRight="380dp" --> <RadioButton android:id="@+id/auton_radio_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/auton_col" /> <RadioButton android:id="@+id/auton_radio_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/auton_col" /> </RadioGroup>
To elaborate, layout_weight
can be used in two ways.
wrap_content
and give the last view a weight of 1.0dp
or match_parent
and give each view the same weight value. They will share the space equally.To have your background drawable scale, make a new xml that goes in your drawable/
folder that looks like this
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:src="@drawable/auton_col" />
Name it whatever you like (e.g. auton_col_scale.xml) and reference that drawable as your background.
I noticed that using layout weights for the RadioButton's caused them to be aligned off-center, eventhough they definitely each shared 50% of the screen (they were left-aligned). Setting a gravity for each RadioButton caused only the text to be centered /facepalm
The XML below shows how to horizontally align two radiobuttons (could be any views) and center them:
<RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> <RadioButton android:id="@+id/radioyes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/yes" android:checked="false"/> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> <RadioButton android:id="@+id/radiono" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no" android:checked="false"/> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> </RadioGroup>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With