RadioButton android code example

Example 1: get string of radio button in android

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

  private RadioGroup radioGroup;
  private RadioButton radioButton;
  private Button btnDisplay;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.radio);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                // get selected radio button from radioGroup
            int selectedId = radioGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioButton.getText(), Toast.LENGTH_SHORT).show();

        }

    });

  }
}

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>