Android APIs
public class

RadioGroup

extends LinearLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.widget.RadioGroup

Class Overview

This class 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. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.

The selection is identified by the unique id of the radio button as defined in the XML layout file.

XML Attributes

See RadioGroup Attributes, LinearLayout Attributes, ViewGroup Attributes, View Attributes

Also see LinearLayout.LayoutParams for layout attributes.

See Also

Summary

Nested Classes
class RadioGroup.LayoutParams

This set of layout parameters defaults the width and the height of the children to WRAP_CONTENT when they are not specified in the XML file. 

interface RadioGroup.OnCheckedChangeListener

Interface definition for a callback to be invoked when the checked radio button changed in this group. 

XML Attributes
Attribute Name Related Method Description
android:checkedButton getCheckedRadioButtonId() The id of the child radio button that should be checked by default within this radio group. 
[Expand]
Inherited XML Attributes
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
RadioGroup(Context context)
RadioGroup(Context context, AttributeSet attrs)
Public Methods
void addView(View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
void check(int id)

Sets the selection to the radio button whose identifier is passed in parameter.

void clearCheck()

Clears the selection.

RadioGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
int getCheckedRadioButtonId()

Returns the identifier of the selected radio button in this group.

void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
void setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)

Register a callback to be invoked when the checked radio button changes in this group.

void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener)
Register a callback to be invoked when a child is added to or removed from this view.
Protected Methods
boolean checkLayoutParams(ViewGroup.LayoutParams p)
LinearLayout.LayoutParams generateDefaultLayoutParams()
Returns a set of layout parameters with a width of MATCH_PARENT and a height of WRAP_CONTENT when the layout's orientation is VERTICAL.
void onFinishInflate()
Finalize inflating a view from XML.
[Expand]
Inherited Methods
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:checkedButton

The id of the child radio button that should be checked by default within this radio group.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol checkedButton.

Related Methods

Public Constructors

public RadioGroup (Context context)

Added in API level 1

public RadioGroup (Context context, AttributeSet attrs)

Added in API level 1

Public Methods

public void addView (View child, int index, ViewGroup.LayoutParams params)

Added in API level 1

Adds a child view with the specified layout parameters.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child the child view to add
index the position at which to add the child
params the layout parameters to set on the child

public void check (int id)

Added in API level 1

Sets the selection to the radio button whose identifier is passed in parameter. Using -1 as the selection identifier clears the selection; such an operation is equivalent to invoking clearCheck().

Parameters
id the unique id of the radio button to select in this group

public void clearCheck ()

Added in API level 1

Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null.

public RadioGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Added in API level 1

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs the attributes to build the layout parameters from
Returns

public int getCheckedRadioButtonId ()

Added in API level 1

Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

Related XML Attributes
Returns
  • the unique id of the selected radio button in this group

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public void setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)

Added in API level 1

Register a callback to be invoked when the checked radio button changes in this group.

Parameters
listener the callback to call on checked state change

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

Added in API level 1

Register a callback to be invoked when a child is added to or removed from this view.

Parameters
listener the callback to invoke on hierarchy change

Protected Methods

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

Added in API level 1

protected LinearLayout.LayoutParams generateDefaultLayoutParams ()

Added in API level 1

Returns a set of layout parameters with a width of MATCH_PARENT and a height of WRAP_CONTENT when the layout's orientation is VERTICAL. When the orientation is HORIZONTAL, the width is set to WRAP_CONTENT and the height to WRAP_CONTENT.

Returns
  • a set of default layout parameters or null

protected void onFinishInflate ()

Added in API level 1

Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added.

Even if the subclass overrides onFinishInflate, they should always be sure to call the super method, so that we get called.