Introduction
In this article, we will learn how to use RadioGroup to manage multiple RadioButtons in an Android application. We will check one example to understand how it works in an android application.
What is a RadioGroup?
A RadioGroup is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group. Intially, all of the radio buttons are unchecked.
Pre-requisites
This article is for beginners who have just started developing android apps and want to know the basics. This post may not help you in real-world project development but, is a short working demonstration of RadioGroup and RadioButton control in Android.
RadioGroup in Android
I have developed a very simple example using RadioGroup, I have dragged two TextViews, five RadioButtons and a RadioGroup to my activity_my.xml. Furthermore, some backgroud colouring was added using layout editor in Android Studio.
In the code, setOnCheckedChangeListener listens to onCheckedChanged events on the RadioGroup. A very simple logic is added to identify which RadioButton was selected.
activity_my.xml
<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=".MyActivity"
android:id="@+id/radio"
android:background="#ffc5bdff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Select your favoutire movie Genre"
android:id="@+id/textViewSelection"
android:layout_above="@+id/radioButtonActionMovies"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="70dp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:layout_alignTop="@+id/textViewSelection"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action Movies"
android:id="@+id/radioButtonActionMovies"
android:layout_above="@+id/radioButtonAnimationMovies"
android:layout_alignLeft="@+id/radioButtonAnimationMovies"
android:layout_alignStart="@+id/radioButtonAnimationMovies"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animation Movies"
android:id="@+id/radioButtonAnimationMovies"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Horror Movies"
android:id="@+id/radioButtonHorrorMovies"
android:layout_below="@+id/radioButtonAnimationMovies"
android:layout_alignLeft="@+id/radioButtonAnimationMovies"
android:layout_alignStart="@+id/radioButtonAnimationMovies"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comedy Movies"
android:id="@+id/radioButtonComedyMovies"
android:layout_below="@+id/radioButtonHorrorMovies"
android:layout_alignLeft="@+id/radioButtonHorrorMovies"
android:layout_alignStart="@+id/radioButtonHorrorMovies"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sci-Fi Movies"
android:id="@+id/radioButtonSciFiMovies"
android:layout_below="@+id/radioButtonComedyMovies"
android:layout_alignLeft="@+id/radioButtonComedyMovies"
android:layout_alignStart="@+id/radioButtonComedyMovies"
android:checked="false" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="You Selected: "
android:id="@+id/textViewChoice"
android:layout_below="@+id/radioGroup"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold"
android:theme="@android:style/Animation.Toast" />
</RelativeLayout>
MyActivity.java
package app.radiobuttoncode.about.me.radiobutton;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity extends ActionBarActivity {
public RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb=(RadioButton)findViewById(checkedId);
textViewChoice.setText("You Selected "+rb.getText());
}
});
}
}