Android radio buttons example

In Android, you can use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected.

In this tutorial, we show you how to use XML to create two radio buttons, and grouped in a radio group. When button is clicked, display which radio button is selected.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. Custom String

Open “res/values/strings.xml” file, add some custom string for radio button.

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, MyAndroidAppActivity!</string>
    <string name="app_name">MyAndroidApp</string>
    <string name="radio_male">Male</string>
    <string name="radio_female">Female</string>
    <string name="btn_display">Display</string>

2. RadioButton

Open “res/layout/main.xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:layout_height="wrap_content" >
            android:checked="true" />
            android:text="@string/radio_female" />
        android:text="@string/btn_display" />
Radio button selected by default.
To make a radio button is selected by default, put android:checked="true" within the RadioButton element. In this case, radio option “Male” is selected by default.

3. Code Code

Inside activity “onCreate()” method, attach a click listener on button.

File :

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 radioSexGroup;
  private RadioButton radioSexButton;
  private Button btnDisplay;
  public void onCreate(Bundle savedInstanceState) {
  public void addListenerOnButton() {
	radioSexGroup = (RadioGroup) findViewById(;
	btnDisplay = (Button) findViewById(;
	btnDisplay.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		        // get selected radio button from radioGroup
			int selectedId = radioSexGroup.getCheckedRadioButtonId();
			// find the radiobutton by returned id
		        radioSexButton = (RadioButton) findViewById(selectedId);
				radioSexButton.getText(), Toast.LENGTH_SHORT).show();

4. Demo

Run the application.

1. Result, radio option “Male” is selected.

android radio button demo1

2. Select “Female” and click on the “display” button, the selected radio button value is displayed.

android radio button demo2

Download Source Code

Download it – (15 KB)


  1. Android RadioGroup JavaDoc
  2. Android RadioButton JavaDoc
  3. Android RadioGroup example
  4. Android RadioButtons example
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: plan()

  • Pingback: payment plan()

  • Pingback: HD Coloring Pages()

  • Pingback: house blue()

  • Pingback: water ionizer loans()

  • Pingback: gene d plumber hoboken()

  • Pingback: pay per day loan plans()

  • Pingback: pay per day loans plan()

  • Pingback: laan her og nu()

  • Pingback: alkaline water machine()

  • Pingback: laan penge nu og her()

  • Pingback: car parking()

  • Pingback: sms lan nu()

  • Pingback: car parking()

  • Pingback: mp4 mobile porn()

  • Pingback: tv packages()

  • Pingback: great prices on DIRECTV()

  • Pingback: kagen water()

  • Pingback: kangen water machine()

  • Pingback: best bottled water()

  • Pingback: water ionizer()

  • Pingback: tv online, online tv()

  • Pingback: watch tv show episodes()

  • Pingback: streaming movies()

  • Pingback: watch movies online()

  • Pingback: Details()

  • Pingback: Blue Coaster33()

  • Mehdi

    Sir, i want to insert this radio button value to database.Can you plz just send me the code?Any help from your side will be appreciable..

  • Amandeep

    good website…thnx

  • saf1

    Thanks for the cool example.

  • Ashwini.M

    Your Site is simply Superb!Thanks for sharing

  • feriyal

    sir, how if i want open a new layout activity after click the other radio buttons ?
    the code please :)

  • http://- hitesh

    sir, How to select Radio button without click on Display button?

    • raja


  • http://- hitesh

    sir, How to select radio button without click on Display button.

  • Rick

    How can I call another activity from selected radio button ?

  • Neo

    A good introductory post for RadioButtons.
    Just one thing how can i use icons instead of text with radioButtons

  • jaro

    Very clear tutorial, thanks for sharing it!

    Just one thing…: radioGender sounds more professional than radioSex


  • Suraj Shinde

    very nice sir…..i like very much this tutorial.thank you sir

  • kurukshetran

    Thanks Sir.Could you help me to solve this problem.

    The problem: 1.If i select 3 in Decks radio group the 15 in the cards radio group should be disabled. Users are not allowed to select 15 and 12 should be the default selected button.

    For example,

    DECKS O3 O4 O5

    CARDS O12 O15

    if 3 or 4 is selected

    then 15 should be disabled

    and 12 should be the default selected button
    if 5 is selected

    then 12 should be disabled

    and 15 should be the default selected button

  • pAT

    Good Job sir!

    very useful

  • Punit

    After selecting any one and clicking on ok than you can’t select the above one. Why it so ?

  • Davis Strauss Maizing

    Thanks !
    Nice and useful tutorial.

  • Lydia

    Thank you very much for the tutorial, it has been very helpful.
    Now I’m trying to display the result in two different activities, one for each gender.


  • Abdul Latheef

    Very nice example
    With regards

  • Srivathsan

    Nice tutorial. can easily use the radio button…

  • Chirag Desai

    Thanks for sharing….
    As I was adding radio buttons run time, I was getting problems to get value for each radio button… this helped me for my project…:)