My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Android RadioButton Control

A RadioButton is a specific type of two-states button that can be either checked or unchecked.

Syntax :


<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Registration"
        android:checked="true"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:textStyle="bold" />

Example :


<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Languages"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CFD8DC" >

        <RadioButton
            android:id="@+id/rbJava"
            android:layout_width="142dp"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="JAVA"
            android:textColor="@android:color/holo_red_light"
            android:textSize="25dp" />

        <RadioButton
            android:id="@+id/rbAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="ANDROID"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="25dp" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFAB91"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rbMale"
            android:layout_width="142dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:checked="true"
            android:text="Male"
            android:textColor="@android:color/holo_red_light"
            android:textSize="25dp" />

        <RadioButton
            android:id="@+id/rbFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:checked="false"
            android:text="Female"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="25dp" />
    </RadioGroup>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:text="Submit"
        android:textColor="#FFF" />

</LinearLayout>

RESULT :

RadioButton Attributes

Attribute Description
android:id This is the ID which uniquely identifies the control.
android:text Text to display.
android:textAllCaps Present the text in ALL CAPS. Possible value either true or false.
android:textColor Text color. May be a color value, in the form of #rgb, #argb, #rrggbb, or #aarrggbb.
android:textSize Size of the text. Recommended dimension type for text is sp for scaled-pixels (example: 15sp).
android:textStyle Style (bold, italic, bolditalic) for the text. You can use or more of the following values separated by '|'.
android:typeface Typeface (normal, sans, serif, monospace) for the text. You can use or more of the following values separated by '|'.
android:checked Indicates the initial checked state of this text.

Android - Layout