In android, Radio Group is used to group one or more radio buttons into separate groups based on our requirements.
If we group Radio Buttons using RadioGroup, at a time only one item can be selected from the group of radio buttons. In case, if we select one radio button that belongs to a radio group will unselect all other previously selected radio buttons within the same group.
Initially, all the radio buttons of the radio group are in the unchecked state, once we select a radio button then it’s not possible for us to uncheck it like CheckBox control.
Following is the pictorial representation of grouping RadioButton controls using RadioGroup in android applications.
Generally, we use radio buttons within a RadioGroup to combine multiple Radio Buttons into one group and it will make sure that user can select only one option from the group of multiple options.
Following is the sample way to define RadioButton control using RadioGroup in the XML layout file in the android application.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"/>
</RadioGroup>
</RelativeLayout>
This is how we can define RadioButton controls in RadioGroup based on our requirements in android applications.
Following is the example of defining multiple RadioButton controls in RadioGroup, one TextView control and one Button control in RelativeLayout to get the selected values of RadioButton controls when we click on Button in the android application.
Create a new android application using android studio and give names as RadioButtonExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Course"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
<RadioButton
android:id="@+id/rdbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAngular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="AngularJS"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_below="@+id/rdGroup"
android:text="Get Course" />
</RelativeLayout>
If you observe above code we created a multiple RadioButton controls in RadioGroup, one TextView control and one Button control in XML Layout file.
Once we are done with the creation of layout with required controls, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.radiogroupexample path and write the code like as shown below.
If you observe above code we are calling our layout using setContentView method in the form of R.layout.layout_file_name in our activity file. Here our xml file name is activity_main.xml so we used file name activity_main and we are getting the status of RadioButton controls when they Select / Deselect and getting selected RadioButton control value on Button click.
Generally, during the launch of our activity, the onCreate() callback method will be called by the android framework to get the required layout for an activity.
When we run the above example using an android virtual device (AVD) we will get a result like as shown below.
If you observe the above result, we are able to select only one option from a set of values and getting the selected RadioButton value on Button click.
This is how we can use RadioButton control in RadioGroup to allow users to select only one option from a set of values in android applications.