{"id":914,"date":"2016-02-15T06:30:27","date_gmt":"2016-02-15T06:30:27","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=914"},"modified":"2019-06-12T10:08:24","modified_gmt":"2019-06-12T10:08:24","slug":"radiobutton","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/radiobutton","title":{"rendered":"RadioButton &#038; RadioGroup Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one radio button out of several radio button added\u00a0in it will automatically unchecked all the others. It means at one\u00a0time we can checked only one radio button from a group of radio buttons which belong to\u00a0same radio group. The most common use of radio button is in <a href=\"\/sourcecode\/quiz\">Quiz Android App code<\/a>.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-980\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-300x169.jpg\" alt=\"RadioButton\" width=\"300\" height=\"169\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-300x169.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton.jpg 443w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center>RadioButon is a two state button that can be checked or unchecked. If a radio button is unchecked then a user can check it by simply clicking on it. Once a RadiaButton is checked by user it can\u2019t be unchecked by simply pressing on the same button. It will automatically unchecked when you press any other RadioButton within same RadioGroup.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important\u00a0Note: <\/strong><\/span>RadioGroup is a widget used in Android for the grouping of radio buttons and provide the feature\u00a0of selecting only one radio button from the set.\u00a0When a user try to select any other radio button within same radio group the previously selected radio button will be automatically unchecked.<\/p>\n<p><strong>RadioGroup And RadioButton code in XML:<\/strong><\/p>\n<pre>&lt;RadioGroup\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"&gt;\r\n        &lt;RadioButton\r\n        android:id=\"@+id\/simpleRadioButton\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\/&gt;\r\n        &lt;RadioButton\r\n        android:id=\"@+id\/simpleRadioButton1\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\/&gt;\r\n&lt;\/RadioGroup&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-917\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-in-Android.jpg\" alt=\"RadioButton in Android\" width=\"316\" height=\"224\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-in-Android.jpg 316w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-in-Android-300x213.jpg 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/center><span style=\"color: #008000;\"><strong>Checking Current State Of Radio Button:<\/strong><\/span><\/p>\n<p>You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either\u00a0true or false. if it\u00a0is checked then returns true otherwise returns false. Below is an example code with explanation in which we checked the current state of a radio button.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nRadioButton simpleRadioButton = (RadioButton) findViewById(R.id.<strong><em>simpleRadioButton<\/em><\/strong>); \/\/ initiate a radio button\r\n\r\nBoolean RadioButtonState = simpleRadioButton.isChecked(); \/\/ check current state of a radio button (true or false).\r\n<\/pre>\n<hr \/>\n<p>&nbsp;<\/p>\n<h4><strong>Attributes of RadioButton In Android:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss important\u00a0attributes that helps us to create\u00a0a beautiful radio button in xml file (layout).<\/p>\n<p><strong><span style=\"color: #008000;\">1. id:<\/span>\u00a0<\/strong>id is an attribute used to uniquely identify a radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\/&gt;<\/pre>\n<p><strong><span style=\"color: #008000;\">2. checked:<\/span>\u00a0<\/strong>checked attribute in radio button is used to set the current state of a radio button. We can set it either true or false\u00a0where true shows the checked state and false shows unchecked state of a radio button. As usual\u00a0default value of checked attribute is false. We can also set the current state in JAVA.<\/p>\n<p>Below we set true value for checked attribute which\u00a0sets the current state to checked of a Button<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\" \r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\/&gt; &lt;!-- set the current state of the radio button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-916\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Checked-Attribute-in-RadioButton-Android.jpg\" alt=\"Checked Attribute in RadioButton Android\" width=\"221\" height=\"173\" \/><\/center><strong>Setting checked State Of RadioButton In Java Class:<\/strong><\/p>\n<p>Below code set the current state of RadioButton to checked programmatically.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate a radio button\r\nRadioButton simpleRadioButton = (RadioButton) findViewById(R.id.<strong><em>simpleRadioButton<\/em><\/strong>);\r\n\r\n\/\/ set the current state of a radio button\r\nsimpleRadioButton.setChecked(<strong>true<\/strong>);<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. text:<\/strong><\/span> text attribute is used to set the text in a radio button. We can set the text both ways either in XML\u00a0or in\u00a0JAVA\u00a0class.<\/p>\n<p>Below is the example code with explanation included in which we set the text \u201cI am a radiobutton\u201d of a\u00a0 radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"I am a radiobutton\" \/&gt; &lt;!-- displayed text of radio button--&gt;<\/pre>\n<p><strong>Setting text of RadioButton In Java class:<\/strong><\/p>\n<p>Below we set the text of a radio button programmatically:<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nRadioButton simpleRadioButton=(RadioButton) findViewById(R.id.simpleRadioButton);\r\nsimpleRadioButton.setText(\"I am a radiobutton\"); \/\/ displayed text of radio button<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-918\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/text-attribute-in-RadioButton-Android.jpg\" alt=\"text attribute in RadioButton Android\" width=\"316\" height=\"243\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/text-attribute-in-RadioButton-Android.jpg 316w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/text-attribute-in-RadioButton-Android-300x231.jpg 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/center><strong><span style=\"color: #008000;\">4. gravity:<\/span>\u00a0<\/strong>The gravity attribute is an optional attribute which is used to control the alignment of text like left, right, center, top, bottom, center_vertical, center_horizontal etc.<\/p>\n<p>Below is the example code with explanation included in which we set the center gravity for the text of a radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"I am a Button\"\r\n    android:gravity=\"center\"\/&gt; &lt;!-- center gravity of the text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-919\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-RadioButton-Android.jpg\" alt=\"gravity in RadioButton Android\" width=\"315\" height=\"225\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-RadioButton-Android.jpg 315w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-RadioButton-Android-300x214.jpg 300w\" sizes=\"auto, (max-width: 315px) 100vw, 315px\" \/><\/center><span style=\"color: #008000;\"><strong>5. textColor:<\/strong><\/span>\u00a0textColor attribute is used to set the text color of a radio button. Color value is in the form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<p>Below we set the red color for the displayed text of a radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"Male\"\r\n    android:textColor=\"#f00\" \/&gt;&lt;!--red color for displayed text--&gt;<\/pre>\n<p><center>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-920\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColor-in-RadioButton.jpg\" alt=\"textColor in RadioButton\" width=\"316\" height=\"234\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textColor-in-RadioButton.jpg 316w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textColor-in-RadioButton-300x222.jpg 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/center><strong>Setting textColor of RadioButton text In Java class:<\/strong><\/p>\n<p>Below we set the text color of a radio button programmatically.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nRadioButton simpleRadioButton = (RadioButton) findViewById(R.id.<strong><em>simpleRadioButton<\/em><\/strong>);\/\/ initiate radio button\r\n\r\nsimpleRadioButton.setTextColor(Color.<strong><em>RED<\/em><\/strong>); \/\/red color for displayed text of radio button<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. textSize:<\/strong><\/span>\u00a0textSize attribute is used to set the size of the text of a radio button. We can set the text size in sp(scale independent pixel) or dp(density pixel).<\/p>\n<p>Below we set the 25sp size for the text of a radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"AbhiAndroid\"\r\n    android:textColor=\"#f00\"\r\n    android:textSize=\"25sp\"\/&gt; &lt;!--setting  text size--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-921\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textsize-attribute-in-RadioButton.jpg\" alt=\"textsize attribute in RadioButton\" width=\"316\" height=\"243\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textsize-attribute-in-RadioButton.jpg 316w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textsize-attribute-in-RadioButton-300x231.jpg 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/center><strong>Setting textSize Of RadioButton Text In Java class:<\/strong><\/p>\n<p>Below we set the text size of a radio button programmatically:<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nRadioButton simpleRadioButton = (RadioButton) findViewById(R.id.<strong><em>simpleRadioButton<\/em><\/strong>); \/\/ initiate radio button\r\n\r\nsimpleRadioButton.setTextSize(25); \/\/ set 25sp displayed text size of radio button<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. textStyle:<\/strong><\/span>\u00a0textStyle attribute is used to set the text style of the text of a radio button. The possible text styles are bold, italic and normal. If we need to use two or more styles for a text view then \u201c|\u201d operator is used for that.<\/p>\n<p>Below is the example code with explanation included in which we set the bold and italic text styles for text of a radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textSize=\"25sp\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"Male\"\r\n    android:textColor=\"#f00\"\r\n    android:textStyle=\"bold|italic\"\/&gt; &lt;!-- bold and italic text style--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-922\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-RadioButton.jpg\" alt=\"textStyle in RadioButton\" width=\"319\" height=\"237\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-RadioButton.jpg 319w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-RadioButton-300x223.jpg 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/center><span style=\"color: #008000;\"><strong>8. background:<\/strong><\/span>\u00a0background attribute is used to set the background of a radio button. We can set a color or a drawable in the background of a radio button.<\/p>\n<p>Below we set the black color for the background and red color for the displayed text of a radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textSize=\"25sp\"\r\n    android:textStyle=\"bold|italic\"\r\n    android:padding=\"20dp\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"Male\"\r\n    android:textColor=\"#f00\"\r\n    android:background=\"#000\"\/&gt; &lt;!-- black background for radio button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-923\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/background-in-RadioButton.jpg\" alt=\"background in RadioButton\" width=\"319\" height=\"256\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/background-in-RadioButton.jpg 319w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/background-in-RadioButton-300x241.jpg 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/center><strong>Setting Background Of RadioButton In Java class:<\/strong><\/p>\n<p>Below we set the background color of a radio button programmatically.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nRadioButton simpleRadioButton = (RadioButton) findViewById(R.id.<strong><em>simpleRadioButton<\/em><\/strong>);\r\n\r\nsimpleRadioButton.setBackgroundColor(Color.<strong><em>BLACK<\/em><\/strong>);<\/pre>\n<p><span style=\"color: #008000;\"><strong>9. padding:<\/strong><\/span> padding attribute is used to set the padding from left, right, top or bottom.<\/p>\n<ul>\n<li><strong>paddingRight:<\/strong>\u00a0set the padding from the right side of the radio button<strong>.<\/strong><\/li>\n<li><strong>paddingLeft :<\/strong>\u00a0set the padding from the left side of the radio button<strong>.<\/strong><\/li>\n<li><strong>paddingTop :<\/strong>\u00a0set the padding from the top side of the radio button<strong>.<\/strong><\/li>\n<li><strong>paddingBottom:<\/strong>\u00a0set the padding from the bottom side of the radio button<strong>.<\/strong><\/li>\n<li><strong>Padding:<\/strong>\u00a0set the padding from the all side\u2019s of the radio button<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set\u00a0padding attribute\u00a0of\u00a020dp padding from all the side\u2019s of the radio button.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textSize=\"25sp\"\r\n    android:textStyle=\"bold|italic\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"AbhiAndroid\"\r\n    android:textColor=\"#f00\"\r\n    android:padding=\"40dp\"\/&gt; &lt;!--40dp padding from all the sides of radio button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-951\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-RadioButton.jpg\" alt=\"Padding in RadioButton\" width=\"223\" height=\"225\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-RadioButton.jpg 223w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-RadioButton-150x150.jpg 150w\" sizes=\"auto, (max-width: 223px) 100vw, 223px\" \/><\/center><span style=\"color: #008000;\"><strong>10. drawableBottom, drawableTop, drawableLeft And drawableRight:<\/strong><\/span>\u00a0These attribute draw the drawable to the below of the text of RadioButton.<\/p>\n<p>Below we set the icon to the right\u00a0of the text of a RadioButton.<\/p>\n<pre>&lt;RadioButton\r\n    android:id=\"@+id\/simpleRadioButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textSize=\"25sp\"\r\n    android:padding=\"20dp\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:text=\"AbhiAndroid\"\r\n    android:textColor=\"#f00\"\r\n    android:drawableRight=\"@drawable\/ic_launcher\" \/&gt; &lt;!-- drawable icon at the right of radio button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-952\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/drawableRight-of-Text-on-RadioButton-Android.jpg\" alt=\"drawableRight of Text on RadioButton Android\" width=\"265\" height=\"216\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example Of RadioButton And RadioGroup in Android Studio:<\/strong><\/h4>\n<p>Below is the example of Radiobutton in Android\u00a0where\u00a0we display five radio buttons with background and other attributes. The radio buttons are used to select your favorite WWE\u00a0superstar with one \u201csubmit\u201d button. Below is the final output, download code and step by step explanation of tutorial:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/RadioButtonExample\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow noopener\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-979\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-And-RadioGroup-Example-in-Android-Studio.jpg\" alt=\"RadioButton And RadioGroup Example in Android Studio\" width=\"224\" height=\"393\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-And-RadioGroup-Example-in-Android-Studio.jpg 224w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-And-RadioGroup-Example-in-Android-Studio-171x300.jpg 171w\" sizes=\"auto, (max-width: 224px) 100vw, 224px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it RadioButtonExample<\/p>\n<p>Select File -&gt; New -&gt; New Project and\u00a0Fill the forms and click &#8220;Finish&#8221; button.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt;\u00a0activity_main.xml (or) main.xml\u00a0and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying 5 RadioButton and one normal button.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:background=\"#e0e0e0\"\r\n        android:orientation=\"vertical\"&gt;\r\n\r\n        &lt;TextView\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:text=\"Select your favourite wwe SuperStar :: \"\r\n            android:textColor=\"#000\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold\" \/&gt;\r\n\r\n        &lt;RadioGroup\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"&gt;\r\n\r\n            &lt;RadioButton\r\n                android:id=\"@+id\/johnCena\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_marginLeft=\"20dp\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:checked=\"true\"\r\n                android:text=\"@string\/johnCena\"\r\n                android:textColor=\"#154\"\r\n                android:textSize=\"20sp\"\r\n                android:textStyle=\"bold\" \/&gt;\r\n\r\n            &lt;RadioButton\r\n                android:id=\"@+id\/randyOrton\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_marginLeft=\"20dp\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:checked=\"false\"\r\n                android:text=\"@string\/randyOrton\"\r\n                android:textColor=\"#154\"\r\n                android:textSize=\"20sp\"\r\n                android:textStyle=\"bold\" \/&gt;\r\n\r\n            &lt;RadioButton\r\n                android:id=\"@+id\/romanReigns\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_marginLeft=\"20dp\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:checked=\"false\"\r\n                android:text=\"@string\/romanReigns\"\r\n                android:textColor=\"#154\"\r\n                android:textSize=\"20sp\"\r\n                android:textStyle=\"bold\" \/&gt;\r\n\r\n            &lt;RadioButton\r\n                android:id=\"@+id\/goldBerg\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_marginLeft=\"20dp\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:checked=\"false\"\r\n                android:text=\"@string\/goldBerg\"\r\n                android:textColor=\"#154\"\r\n                android:textSize=\"20sp\"\r\n                android:textStyle=\"bold\" \/&gt;\r\n\r\n\r\n            &lt;RadioButton\r\n                android:id=\"@+id\/sheamus\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_marginLeft=\"20dp\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:checked=\"false\"\r\n                android:text=\"@string\/sheamus\"\r\n                android:textColor=\"#154\"\r\n                android:textSize=\"20sp\"\r\n                android:textStyle=\"bold\" \/&gt;\r\n\r\n        &lt;\/RadioGroup&gt;\r\n\r\n        &lt;Button\r\n            android:id=\"@+id\/submitButton\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_gravity=\"center\"\r\n            android:layout_margin=\"20dp\"\r\n            android:background=\"#0f0\"\r\n            android:padding=\"10dp\"\r\n            android:text=\"Submit\"\r\n            android:textColor=\"#fff\"\r\n            android:textSize=\"20sp\"\r\n            android:textStyle=\"bold\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open \u00a0src -&gt; package -&gt;\u00a0<strong>MainActivity.<\/strong><strong>java<\/strong><\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code to\u00a0initiate the RadioButton and normal button. We also perform click event on button and display the selected superstar&#8217;s name by using a Toast.<\/p>\n<pre>package example.gb.radiobuttonexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.RadioButton;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    RadioButton johnCena, randyOrton, goldBerg, romanReigns, sheamus;\r\n    String selectedSuperStar;\r\n    Button submit;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        johnCena = (RadioButton) findViewById(R.id.johnCena);\r\n        randyOrton = (RadioButton) findViewById(R.id.randyOrton);\r\n        goldBerg = (RadioButton) findViewById(R.id.goldBerg);\r\n        romanReigns = (RadioButton) findViewById(R.id.romanReigns);\r\n        sheamus = (RadioButton) findViewById(R.id.sheamus);\r\n        submit = (Button) findViewById(R.id.submitButton);\r\n        submit.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                if (randyOrton.isChecked()) {\r\n                    selectedSuperStar = randyOrton.getText().toString();\r\n                } else if (sheamus.isChecked()) {\r\n                    selectedSuperStar = sheamus.getText().toString();\r\n                } else if (johnCena.isChecked()) {\r\n                    selectedSuperStar = johnCena.getText().toString();\r\n                } else if (romanReigns.isChecked()) {\r\n                    selectedSuperStar = romanReigns.getText().toString();\r\n                } else if (goldBerg.isChecked()) {\r\n                    selectedSuperStar = goldBerg.getText().toString();\r\n                }\r\n                Toast.makeText(getApplicationContext(), selectedSuperStar, Toast.LENGTH_LONG).show(); \/\/ print the value of selected super star\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Open res -&gt; values -&gt;\u00a0<strong>strings.<\/strong><strong>xml<\/strong><\/p>\n<p>In this step we open String file\u00a0which is\u00a0used to store string data of the app.<strong>\u00a0<\/strong><\/p>\n<pre>&lt;resources&gt;\r\n    &lt;string name=\"app_name\"&gt;RadioButtonExample&lt;\/string&gt;\r\n    &lt;string name=\"hello_world\"&gt;Hello world!&lt;\/string&gt;\r\n    &lt;string name=\"action_settings\"&gt;Settings&lt;\/string&gt;\r\n    &lt;string name=\"randyOrton\"&gt;Randy Orton&lt;\/string&gt;\r\n    &lt;string name=\"johnCena\"&gt;John Cena&lt;\/string&gt;\r\n    &lt;string name=\"romanReigns\"&gt;Roman Reigns&lt;\/string&gt;\r\n    &lt;string name=\"goldBerg\"&gt;Gold Berg&lt;\/string&gt;\r\n    &lt;string name=\"sheamus\"&gt;Sheamus&lt;\/string&gt;\r\n&lt;\/resources&gt;<\/pre>\n<p><strong>\u00a0Run The App:<\/strong><\/p>\n<p>Now run the App in Emulator and you will see 5 RadioButton in RadioGroup listing WWE superstar name. Now choose your favorite one and click on Submit Button. The name will be displayed on Screen.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-979\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-And-RadioGroup-Example-in-Android-Studio-171x300.jpg\" alt=\"RadioButton And RadioGroup Example App Output\" width=\"171\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-And-RadioGroup-Example-in-Android-Studio-171x300.jpg 171w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/RadioButton-And-RadioGroup-Example-in-Android-Studio.jpg 224w\" sizes=\"auto, (max-width: 171px) 100vw, 171px\" \/><\/center><strong>Also check: <a href=\"\/sourcecode\/quiz\">Quiz Android App Source Code<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one radio button out of several radio button added\u00a0in it will automatically unchecked all the others. It means at one\u00a0time we can checked only one radio button from a group of radio buttons which belong to\u00a0same radio group. The most common &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/radiobutton\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RadioButton &#038; RadioGroup Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-914","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>RadioButton &amp; RadioGroup Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"In Android, RadioButton are mainly used together in a RadioGroup. Follow the tutorial to learn the topic with example in Android Studio.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/radiobutton\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=914"}],"version-history":[{"count":4,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/914\/revisions"}],"predecessor-version":[{"id":2794,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/914\/revisions\/2794"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}