android radio button selected code example

Example 1: radiogroup get selected item android

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);

Example 2: radio button android:inputType

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

<RadioGroup
    android:id="@+id/radioUnits"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:text="@string/M"
        android:id="@+id/M"
        android:layout_gravity="center_horizontal"
        android:checked="true" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/mM"
        android:inputType="text"
        android:id="@+id/mM"
        android:layout_gravity="center_horizontal"/>
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/uM"
        android:id="@+id/uM"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.09"/>
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/pM"
        android:id="@+id/pM"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.09"/>
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/cM"
        android:id="@+id/cM"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.09"/>

</RadioGroup>

    <Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Set"
    android:layout_weight="0.15"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="68dp"
        android:layout_marginBottom="133dp" />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_gravity="right"
        android:layout_alignTop="@+id/b1"
        android:layout_toRightOf="@+id/b1"
        android:layout_toEndOf="@+id/b1"
        android:layout_marginLeft="77dp" />

</RelativeLayout>

Tags:

Java Example