In this article, we will see an example of Android’s RadioButton.
Unlike a checkbox, once checked, a radio button cannot be unchecked. Radio buttons are usually used in a group represented by RadioGroup. Each group provides options to the user. On selecting one radio button, it will automatically deselect all the others thus radio buttons in a group are mutually exclusive which means only one radio button can be selected at a time.
Let’s begin with our example.
Main Layout
Our layout consists of a title, radio group and the radio buttons within the group, and a button.
We will click on the button and the listener attached to the button will print the radio button selected.
Few points to note about Radio Button.
- To create each radio button option, create a RadioButton in your layout.
- Since the radio buttons are the options from which user will select one, you must group them together inside a RadioGroup.
- Use
android:onClick
to listen to the on-click event. You must mention the name of the Activity’s method that you want to call in response to a click event - Set
android:checked
to true to check the button
welcome.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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.javarticles.android.RadioGroupExampleActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_group_rate_your_trip" /> <RadioGroup android:id="@+id/rating_group" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/rating_good_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:checked="true" android:onClick="onRatingClicked" android:text="@string/radio_btn_good" /> <RadioButton android:id="@+id/radio_bad_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onRatingClicked" android:text="@string/radio_btn_bad" /> <RadioButton android:id="@+id/rating_average_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onRatingClicked" android:text="@string/radio_btn_average" /> </RadioGroup> <Button android:id="@+id/rating_done_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/rating_group" android:layout_below="@+id/rating_group" android:layout_marginTop="43dp" android:text="@string/rating_done_btn" /> </RelativeLayout>
Main Activity
Few points to note about the activity.
RadioGroup.getCheckedRadioButtonId()
will let us know which radio button is checked.- Given a
RadioButton
, callRadioButton.isChecked()
to know whether a button is checked. - Since we have set
android:onClick
toonRatingClicked
method, it will get called as the radio button is checked.
RadioGroupExampleActivity:
package com.javarticles.android; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class RadioGroupExampleActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); Button doneBtn = (Button) findViewById(R.id.rating_done_btn); final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rating_group); doneBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int checkedRadioBtn = radioGroup.getCheckedRadioButtonId(); RadioButton radioButton = (RadioButton) findViewById(checkedRadioBtn); Toast.makeText(v.getContext(), "You have rated " + radioButton.getText(), Toast.LENGTH_SHORT).show(); } }); } public void onRatingClicked(View view) { // Is the button now checked? boolean checked = ((RadioButton) view).isChecked(); // Check which radio button was clicked switch (view.getId()) { case R.id.rating_good_btn: Toast.makeText(view.getContext(), "You have selected Good", Toast.LENGTH_SHORT).show(); break; case R.id.rating_average_btn: Toast.makeText(view.getContext(), "You have selected Average", Toast.LENGTH_SHORT).show(); break; case R.id.radio_bad_btn: Toast.makeText(view.getContext(), "You have selected Bad", Toast.LENGTH_SHORT).show(); break; } } }
Run the application
Rate your trip by selecting one option. Default is set to ‘Good’.
Check one of the RadioButton.
Press on Done when you are done with the selection.
Download the source code
This was an example about Android’s RadioButton. You can download the source code here: radioGroupExample.zip