In this tutorial you will learn how to create a RadioButton, which allows user to choose one of several options. Putting multiple RadioButton elements in one container makes it impossible to choose more than one option, which means that when user toggles one button it automatically cancels the previous one. There is only two things that you will need to do to have a RadioButton element in your application: 1. Add RadioButton element to xml layout file and 2. Add onClick() event handler to your activity class.
To add this element to your application you need to open xml layout file (which is located in the res\layout directory) and add the following code to it:
activity_main.xml file
1. Adding Android RadioButton element
To add this element to your application you need to open xml layout file (which is located in the res\layout directory) and add the following code to it:
activity_main.xml file
<RadioGroupxmlns: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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:background="#9000"tools:context=".MainActivity"><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#FFFFFF"android:text="@string/hint"/><RadioButtonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/yellow"android:textColor="#FFD700"/><RadioButtonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/red"android:textColor="#FF6347"/><RadioButtonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/blue"android:textColor="#4169E1"/><TextViewandroid:id="@+id/text2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#ffffff"android:text="@string/empty"/></RadioGroup>
As you have probably noticed I am using five string resources in this file. To add them go to res\values directory and add the following lines to your strings.xml file:
<stringname="yellow">Yellow</string><stringname="red">Red</string><stringname="blue">Blue</string><stringname="hint">Select one of the colors:</string><stringname="empty"> </string>
2. Adding onClick() event handler
Now to find out which option has been chosen we need to add onClick() event handler to each of our RadioButtons. To do that we need to modify activity class:
MainActivity.java file
packageradiobuttonexample.tuts.com;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.RadioButton;importandroid.widget.TextView;public classMainActivityextendsActivity {privateRadioButtonfirst_button;privateRadioButtonsecond_button; private RadioButtonthird_button;privateTextViewtext; @Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);first_button= (RadioButton) findViewById(R.id.button1);second_button= (RadioButton) findViewById(R.id.button2);third_button= (RadioButton) findViewById(R.id.button3);text= (TextView) findViewById(R.id.text2); onClickMethod(); }private voidonClickMethod() { // TODO Auto-generated method stub OnClickListener radio_button_click = (newOnClickListener() { @Override public void onClick(View v) { RadioButtonrb= (RadioButton)v ;text.setText ("You've selected "+ rb.getText()); } });first_button.setOnClickListener(radio_button_click);second_button.setOnClickListener(radio_button_click);third_button.setOnClickListener(radio_button_click); } }
DEMO
![]() |
| Android RadioButton Example |
Source code

