Android Radio Button / Radio Group Example


In this example we will see how to add radio buttons to our android application. We can render a radio button using android.widget.RadioButton class ,but having a single radio button is of no use. Android provides android.widget.RadioGroup to group the radio buttons so that if one radio button is selected within a group others are deselected. Here we will design a screen having radio button within a group and toast the selected item.

Lets start:

1. Create an android application with any packagename and project name. Name the activity as AndroidRadioActivity.java and the layout file as main.xml
2.Let us design the layout file with a TextView posting a question followed by radio buttons and a display button.

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How do you rate Android?" />

    <RadioGroup
        android:id="@+id/radioOptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="1" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />

        <RadioButton
            android:id="@+id/radio5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />
    </RadioGroup>

    <Button
        android:id="@+id/btnDisplayRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display Rating" />

</LinearLayout>
3.Now in the activity file let us display the text associated with the radio button selected on tapping a display rating button.

AndroidRadioActivity:

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 AndroidRadioActivity extends Activity {

 private RadioGroup radioOptions;
 private RadioButton radioButton;
 private Button btnDisplay;

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

  radioOptions = (RadioGroup) findViewById(R.id.radioOptions);
  btnDisplay = (Button) findViewById(R.id.btnDisplayRating);

  btnDisplay.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    int selectedId = radioOptions.getCheckedRadioButtonId();

    radioButton = (RadioButton) findViewById(selectedId);
    Toast.makeText(AndroidRadioActivity.this,
      "You Rated as " + radioButton.getText(),
      Toast.LENGTH_SHORT).show();

   }

  });
 }

}


4.Build the application and run the project. The output would be similar to the one below.

Output: