
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.
RadioGroup Example
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package androindian.radio; 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 { //declaring local variables public RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); //initializing variables radioGroup=(RadioGroup)findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // checkedId is the RadioButton selected RadioButton rb=(RadioButton)findViewById(checkedId); textViewChoice.setText("You Selected "+rb.getText()); //Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show(); } }); } } |