Android RadioGroup Control

0
394

A set of radio buttons combine to make use of the RadioGroup class. The working of a RadioGroup is linked with the working of the each one of radio buttons involved in it. So, if a user checks one of the radio buttons belonging to a Radio Group then it will automatically uncheck any of the previously checked options from the set.

Also Read: Android CheckBox Control

Various RadioGroup Attributes
A few of the major attributes of RadioGroup are listed below. For the complete list of RadioGroup attributes, you can visit the official Android documentation. If you will take a look at this official doc, you will also get to know about the methods that can be used to change the values of the RadioGroup attributes at the runtime.

S. No. Attribute Description
1 android:checkedButton * The value of this attribute is accepted as the id of the child radio button.
* Make sure that you keep the state of this radio button as checked so that this radio group can be enabled.

The below-provided list of RadioGroup attributes is inherited from the android.view.View class.

S. No. Attribute Description
1 android:id * This attribute’s value is considered as the view’s identifier name.
2 android:background * The value of this attribute accepts a drawable value to be kept as the background for the view.
3 android:visibility * The visibility of the view is controlled by this attribute.
4 android:contentDescription * The briefly described content of the view is passed in this attribute.
5 android:onClick * This is the method which gets invoked whenever the view is clicked.

For the better understanding of the use of RadioGroup UI control, take a look at the following example. In here, you will get a step-to-step guide to create an Android application with the help of the Linear layout and RadioGroup control.

Step 1: Open the Android Studio IDE and create an Android application under the name of myapplication. Keep it in the com.example.w3school.myapplication package. This step has already been mentioned in the Hello World Example chapter.

Step 2: The MainActivity.java file already contains the definitions of the fundamental lifecycle callback methods. So, you just need to add a click event to this file. To locate this file, look into the src/ directory.

 package com.example.w3school.myapplication;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; 

public class MainActivity extends Activity
{
   private RadioGroup radioSexGroup;
   private RadioButton radioSexButton;
   private Button btnDisplay;   
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);      
      btnDisplay=(Button)findViewById(R.id.button);      
      btnDisplay.setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            int selectedId=radioSexGroup.getCheckedRadioButtonId();
            radioSexButton=(RadioButton)findViewById(selectedId);            Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
         }
      });
   }
}

Step 3: There is another file which needs a section of code to be included in it. Look under the res/layout/ path and you will find the activity_main.xml file. In here, add all the required Android UI controls. 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns: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:paddingLeft=”@dimen/activity_horizontal_margin”
   android:paddingRight=”@dimen/activity_horizontal_margin”
   android:paddingTop=”@dimen/activity_vertical_margin”
   android:paddingBottom=”@dimen/activity_vertical_margin”
   tools:context=”.MainActivity”>   

   <TextView
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Radio button”
      android:id=”@+id/textView”
      android:layout_alignParentTop=”true”
      android:layout_centerHorizontal=”true”
      android:textSize=”35dp” />   

   <TextView
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Tutorialspoint”
      android:id=”@+id/textView2″
      android:layout_below=”@+id/textView”
      android:layout_alignRight=”@+id/textView”
      android:layout_alignEnd=”@+id/textView”
      android:textSize=”35dp”
      android:textColor=”@android:color/holo_green_dark” />   

   <ImageView
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/imageView”
      android:src=”@drawable/abc”
      android:layout_below=”@+id/textView2″
      android:layout_alignLeft=”@+id/textView”
      android:layout_alignStart=”@+id/textView”
      android:layout_alignRight=”@+id/textView”
      android:layout_alignEnd=”@+id/textView” />   

   <RadioGroup
      android:layout_width=”fill_parent”
      android:layout_height=”90dp”
      android:layout_below=”@+id/imageView”
      android:layout_marginTop=”58dp”
      android:weightSum=”1″
      android:id=”@+id/radioGroup”
      android:layout_alignLeft=”@+id/textView2″
      android:layout_alignStart=”@+id/textView2″
      android:layout_alignRight=”@+id/textView3″
      android:layout_alignEnd=”@+id/textView3″>   

      <RadioButton
         android:layout_width=”wrap_content”
         android:layout_height=”55dp”
         android:text=”Male”
         android:id=”@+id/radioButton”
         android:layout_gravity=”center_horizontal”
         android:checked=”false”
         android:textSize=”25dp” /> 

      <RadioButton
         android:layout_width=”wrap_content”
         android:layout_height=”wrap_content”
         android:text=”Female”
         android:id=”@+id/radioButton2″
         android:layout_gravity=”center_horizontal”
         android:checked=”false”
         android:textSize=”25dp”
         android:layout_weight=”0.13″ />

   </RadioGroup> 
   <TextView
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”    Are you?”
      android:id=”@+id/textView3″
      android:textSize=”35dp”
      android:layout_below=”@+id/imageView”
      android:layout_alignRight=”@+id/textView2″
      android:layout_alignEnd=”@+id/textView2″
      android:layout_alignLeft=”@+id/imageView”
      android:layout_alignStart=”@+id/imageView” />   

   <Button
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”New Button”
      android:id=”@+id/button”
      android:layout_gravity=”center_horizontal”
      android:layout_below=”@+id/radioGroup”
      android:layout_centerHorizontal=”true” />
</RelativeLayout>

Step 4: Let the content of the res/values/strings.xml file remain as it is. The required changes will automatically be done by the Android Studio IDE.

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string name=”app_name”>My Applicaiton</string>
   <string name=”example_radiogroup”>Example showing RadioGroup</string>
</resources>

Step 5: Keep the AndroidManifest.xml file untouched. It does not require any kind of modifications.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.w3school.myapplication” >   
   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” >      

      <activity
         android:name=”com.example.My Application.MainActivity”
         android:label=”@string/app_name” >      
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>      
      </activity>      
   </application>
</manifest> 

Step 6: The last step is to verify all the changes done and check whether the application runs successfully or not. For this, you need to have an already created AVD. We are assuming you are done with this process during the environment setup of the Android Studio. Now, open your project’s activity file and click on the Run button. The Studio then start installing your myapplication app on the AVD and if everything is fine then you will be displayed with the final output on the Emulator window.

The final output screen will display two Radio Buttons, which are Male and Female. A user needs to select either of the two buttons. Once a user selects one of the Radio Buttons and hit on the New Button button control then he/she will be displayed with a toast message.