Pr.Pg Next Pg

Radio Buttons Tutorials

  • We will now introduce the Radio Buttons and Relative Layout.

  • Radio buttons allow the user to select one option from a set.

  • You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side.

  • If it's not necessary to show all options side-by-side, use aspinner instead.

  • Following is a code example of a radio button in XML.

 

 

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

 

<RadioGroup

android:id="@+id/radioGroup1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

 

<RadioButton

android:id="@+id/radio0"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:text="Yes" />

 

<RadioButton

android:id="@+id/radio1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="No" />

 

</RadioGroup>

 

</LinearLayout>

 

  • To create each radio button option, create a RadioButton in your layout.

  • However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup.

  • By grouping them together, the system ensures that only one radio button can be selected at a time.

 

Pr.Pg border                                              Next Pg