Radio buttons are also known as option buttons. A user can choose only one option from a group of mutually exclusive options. This is unlike check boxes which enable a user to select more than one option.
1. In the XML Layout file add RadioButtons inside a RadioGroup.
Example:
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="5dp"
android:text="@string/monical_s_pizza_restaurant"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:minLines="2"
android:text="All of our appetizers are made with natural ingredients."
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="5dp"
android:text="@string/choose_one_of_these_appetizers_"
android:textColor="@android:color/black"
android:textSize="15sp" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:textColor="@android:color/black" >
<RadioButton
android:id="@+id/opt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheese_breadsticks_with_garlic_bbq_sauce"
android:textSize="15sp" />
<RadioButton
android:id="@+id/opt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/spinach_queso_fundido_with_cole_slaw"
android:textSize="15sp" />
<RadioButton
android:id="@+id/opt_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crispy_fries_with_house_speciality_cheddar_cheese"
android:textSize="15sp" />
<RadioButton
android:id="@+id/opt_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mozzarella_stuffed_vegetable_platter"
android:textSize="15sp" />
<RadioButton
android:id="@+id/opt_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/zucchini_squash_mix_with_hawaiian_pineapple_slices"
android:textSize="15sp" />
</RadioGroup>
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="25dp"
android:background="@android:color/holo_orange_light"
android:text="@string/clear" />
</LinearLayout>
2. Declare a RadioGroup and several RadioButton variables and link them to the UI.
Note: The RadioGroup variable is declared final because it is accessed later from an inner class.
Example:
final RadioGroup RG = (RadioGroup) findViewById(R.id.radio_group);
RadioButton RB1 = (RadioButton) findViewById(R.id.opt_1);
RadioButton RB2 = (RadioButton) findViewById(R.id.opt_2);
RadioButton RB3 = (RadioButton) findViewById(R.id.opt_3);
RadioButton RB4 = (RadioButton) findViewById(R.id.opt_4);
RadioButton RB5 = (RadioButton) findViewById(R.id.opt_5);
Button btn_clear = (Button) findViewById(R.id.btn_clear);
3. If you want, you can customize the action bar text and logo.
Example:
ActionBar actionBar = getActionBar();
actionBar.setLogo(drawable.logo);
actionBar.setTitle("");
4. By default the radio buttons are displayed cleared. You can preset any of the options using code such as this:
RB3.setChecked (true);
5. Here is example code to set up the radio buttons:
// Radio Button 1
RB1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this,
"You clicked Radio Button #1", Toast.LENGTH_LONG)
.show();
}
});
// Radio Button 2
RB2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this,
"You clicked Radio Button #2", Toast.LENGTH_LONG)
.show();
}
});
// Radio Button 3
RB3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this,
"You clicked Radio Button #3", Toast.LENGTH_LONG)
.show();
}
});
// Radio Button 4
RB4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this,
"You clicked Radio Button #4", Toast.LENGTH_LONG)
.show();
}
});
// Radio Button 5
RB5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this,
"You clicked Radio Button #5", Toast.LENGTH_LONG)
.show();
}
});
6. You can create a button to clear all choices.
// Clear Button
btn_clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"You clicked the Clear Button", Toast.LENGTH_LONG)
.show();
RG.clearCheck();
}
});
The Android documentation uses a single listener with a switch statement to determine which radio button is selected. Each radio button in the XML Layout file has the same method name in its onClick attribute:
android:onClick="onRadioButtonClicked"
My preference is to handle the radio button listeners entirely in code rather than mix it up with the layout.
The Android docs provides this code as an example of handling radio button clicks. I added the toasts (below).
Note: The commented lines are not needed and I added this as a possible docs bug:
3. (Added) Doc Bug (Severity=1)
https://sites.google.com/site/myrononmobileapps/reference-material/bugs
Official Android example:
http://developer.android.com/guide/topics/ui/controls/radiobutton.html
public void onRadioButtonClicked(View view) {
// Is the button now checked?
// boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
// if (checked)
// Pirates are the best
Toast.makeText(MainActivity.this, "Pirates are the best", Toast.LENGTH_LONG).show();
break;
case R.id.radio_ninjas:
// if (checked)
// Ninjas rule
Toast.makeText(MainActivity.this, "Ninjas rule", Toast.LENGTH_LONG).show();
break;
}
}
}