Browse By

Android Radio Button and Radio Group Example

Create Radio Button and Radio Group Example in Android Studio 2.0.

You often use radio buttons when a user should be allowed to only select one item from a small group of items. For example multiple options for question or Opinion Poll. Only one object should be checked at a time.

In Android, you can use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup.

In this tutorial we will create simple application for Opinion Poll . Application will ask user, how was today’s tutorial ? User will have four options like (Excellent, Good, Average and Poor). User will be able to select only one of the option. Below is summary of task which we will cover in this tutorial.

  1. Display Opinion Options using Radio Button and Radio Group
  2. When user click on submit button will display user opinion on Toast

Direct Download Full Source Code



Android Radio Button and Radio Group Example

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Open your main.xml and design a simple layout for Opinion using Radio Group and Radio Button
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	<TextView android:textSize="20dp" android:textStyle="bold"
    		android:textColor="#fff" android:layout_width="wrap_content"
    		android:layout_gravity="center_horizontal" android:id="@+id/textView1"
    		android:layout_height="wrap_content" android:layout_marginTop="10dp"
    		android:layout_marginBottom="10dp" android:text="How was today's tutorial ?"></TextView>
    	<RadioGroup android:layout_width="wrap_content"
    		android:layout_gravity="center_horizontal" android:layout_height="wrap_content"
    		android:layout_weight="1" android:id="@+id/rgOpinion">
    		<RadioButton android:layout_width="wrap_content"
    			android:id="@+id/radio0" android:layout_height="wrap_content"
    			android:checked="true" android:text="Excellent"></RadioButton>
    		<RadioButton android:id="@+id/radioButton1"
    			android:layout_width="wrap_content" android:layout_height="wrap_content"
    		<RadioButton android:layout_width="wrap_content"
    			android:id="@+id/radio1" android:layout_height="wrap_content"
    		<RadioButton android:layout_width="wrap_content"
    			android:id="@+id/radio2" android:layout_height="wrap_content"
    	<Button android:text="Submit Poll" android:layout_width="match_parent"
    		android:layout_height="wrap_content" android:id="@+id/btnSubmit"></Button>
  3. Now open your Activity Class and put following code.
    package com.vrs.opinionpoll;
    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 Pulse7OpinionPollActivity extends Activity implements
    		OnClickListener {
    	// Widget GUI
    	RadioGroup rgOpinion;
    	Button btnSubmit;
    	/** Called when the activity is first created. */
    	public void onCreate(Bundle savedInstanceState) {
    		// Init Widget GUI
    		rgOpinion = (RadioGroup) findViewById(;
    		btnSubmit = (Button) findViewById(;
    		// Attached Click listener to Button
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		if (v == btnSubmit) {
    			// Get Selected Radio Button and display output
    			RadioButton selectRadio = (RadioButton) findViewById(rgOpinion
    			String opinion = selectRadio.getText().toString();
    			Toast.makeText(this, "Your Opinion is : " + opinion,
  4. Now Execute Application
    Android Radio Button and Radio Group Example

    Execute application, you will get this screen

    Android Radio Button and Radio Group Example

    Select your opinion and press submit button, will display selected opinion on Toast

Points to Remember

  • To get selected Radio Button ID, use  getCheckedRadioButtonId() method.
  • To see Radio Button is selected or not, use  isChecked() method.

I hope you like this article. Share your views to to improve content.

Download Full Source Code