Android RadioGroup with Examples

  Total Views : 180
  Comments : 0
  Keywords : Android radiogroup is a class which is used for multiple radio buttons, Android radiogroup if we select one radiobutton automatically it will unselect all other radio buttons in the group

Here we will learn radiogroup in android with examples and how to use android radiogroup to group multiple radio buttons into single group and make sure to select only one option at a time, get selected radio button value in radiogroup and how to create android radiogroup programmatically or in XML layout file with examples.

Android RadioGroup UI Control

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 in the same group.

 

Initially, all the radio buttons of radio group are in 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 a RadioButton controls using RadioGroup in android applications.

 

Android RadioButtons Example Diagram

 

Generally, we use radio buttons with in 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 XML layout file in 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.

Android RadioGroup Example

Following is the example of defining a 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 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

activity_main.xml

<?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 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.

MainActivity.java

package com.tutlane.radiobuttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    RadioButton
android, java, angular, python;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
android = (RadioButton)findViewById(R.id.rdbAndroid);
       
angular = (RadioButton)findViewById(R.id.rdbAngular);
       
java = (RadioButton)findViewById(R.id.rdbJava);
       
python = (RadioButton)findViewById(R.id.rdbPython);
        Button btn = (Button)findViewById(R.id.
getBtn);
        btn.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String result =
"Selected Course: ";
                result+= (
android.isChecked())?"Android":(angular.isChecked())?"AngularJS":(java.isChecked())?"Java":(python.isChecked())?"Python":"";
                Toast.makeText(getApplicationContext(), result, Toast.
LENGTH_SHORT).show();
            }
        });
    }
   
public void onRadioButtonClicked(View view) {
       
boolean checked = ((RadioButton) view).isChecked();
        String str=
"";
       
// Check which radio button was clicked
       
switch(view.getId()) {
           
case R.id.rdbAndroid:
                
if(checked)
                str =
"Android Selected";
               
break;
           
case R.id.rdbAngular:
               
if(checked)
                str =
"AngularJS Selected";
               
break;
           
case R.id.rdbJava:
                
if(checked)
                str =
"Java Selected";
               
break;
           
case R.id.rdbPython:
               
if(checked)
                str =
"Python Selected";
               
break;
        }
        Toast.makeText(getApplicationContext(), str, Toast.
LENGTH_SHORT).show();
    }
}

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, onCreate() callback method will be called by android framework to get the required layout for an activity.

Output of Android RadioGroup Example

When we run above example using android virtual device (AVD) we will get a result like as shown below.

 

Android RadioButtons Example Result

 

If you observe above result, we are able to select only one option from 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 set of values in android applications.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User