Radiobutton with text above button
The @style/TabStyle
is simply a style that is applied, you can ignore that. The @drawable/main_selector
is a graphic that is toggled depending on the situation. You can read more about selectors here.
Example to get text on top:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:text="Text on top"
android:button="@null"
android:background="#f00"
android:layout_weight="1"/>
<RadioButton
android:text="Text on top"
android:button="@null"
android:background="#0f0"
android:layout_weight="1"/>
</RadioGroup>
Will give following result:
If you want the Text to appear above the button you can use following xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:text="Text on top"
android:button="@null"
android:drawableBottom="@android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
<RadioButton
android:text="Text on top"
android:button="@null"
android:drawableBottom="@android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
</RadioGroup>
This will give following result:
To complement Warpzit great answer, you should use android:gravity="center_horizontal|bottom"
and android:layout_height="match_parent"
on the buttons, to align them.
Also there's no need to copy the radio button drawables from AOSP, use ?android:attr/listChoiceIndicatorSingle
.
XML Layout
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup1"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:text="RadioButton dsfsdfsdfsdfsdf" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:text="RadioButton fdsfsd fdsfsdf fsfsdfs" />
</RadioGroup>