Skip to content

Most visited

Recently visited

navigation

RadioGroup

public class RadioGroup
extends LinearLayout

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


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

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

Inherited XML attributes

From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View

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 autofill(AutofillValue value)

Automatically fills the content of this view with the value.

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.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

int getAutofillType()

Describes the autofill type of this view, so an AutofillService can create the proper AutofillValue when autofilling the view.

AutofillValue getAutofillValue()

Gets the View's current autofill value.

int getCheckedRadioButtonId()

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

void onProvideAutofillStructure(ViewStructure structure, int flags)

Populates a ViewStructure to fullfil an autofill request.

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.

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.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
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.

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

Related methods:

Public constructors

RadioGroup

added in API level 1
RadioGroup (Context context)

Parameters
context Context

RadioGroup

added in API level 1
RadioGroup (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Public methods

addView

added in API level 1
void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

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 View: the child view to add

index int: the position at which to add the child or -1 to add last

params ViewGroup.LayoutParams: the layout parameters to set on the child

autofill

added in API level 26
void autofill (AutofillValue value)

Automatically fills the content of this view with the value.

Views support the Autofill Framework mainly by:

  • Providing the metadata defining what the view means and how it can be autofilled.
  • Implementing the methods that autofill the view.

onProvideAutofillStructure(ViewStructure, int) is responsible for the former, this method is responsible for latter.

This method does nothing by default, but when overridden it typically:

  1. Checks if the provided value matches the expected type (which is defined by getAutofillType()).
  2. Checks if the view is editable - if it isn't, it should return right away.
  3. Call the proper getter method on AutofillValue to fetch the actual value.
  4. Pass the actual value to the equivalent setter in the view.

For example, a text-field view could implement the method this way:

 @Override
 public void autofill(AutofillValue value) {
   if (!value.isText() || !this.isEditable()) {
      return;
   }
   CharSequence text = value.getTextValue();
   if (text != null) {
     this.setText(text);
   }
 }
 

If the value is updated asynchronously, the next call to notifyValueChanged(View) must happen after the value was changed to the autofilled value. If not, the view will not be considered autofilled.

Parameters
value AutofillValue: value to be autofilled.

check

added in API level 1
void check (int id)

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 int: the unique id of the radio button to select in this group

See also:

clearCheck

added in API level 1
void clearCheck ()

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

See also:

generateLayoutParams

added in API level 1
RadioGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

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

Parameters
attrs AttributeSet: the attributes to build the layout parameters from

Returns
RadioGroup.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

added in API level 23
CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getAutofillType

added in API level 26
int getAutofillType ()

Describes the autofill type of this view, so an AutofillService can create the proper AutofillValue when autofilling the view.

By default returns AUTOFILL_TYPE_NONE, but views should override it to properly support the Autofill Framework.

Returns
int either AUTOFILL_TYPE_NONE, AUTOFILL_TYPE_TEXT, AUTOFILL_TYPE_LIST, AUTOFILL_TYPE_DATE, or AUTOFILL_TYPE_TOGGLE.

getAutofillValue

added in API level 26
AutofillValue getAutofillValue ()

Gets the View's current autofill value.

By default returns null, but views should override it to properly support the Autofill Framework.

Returns
AutofillValue

getCheckedRadioButtonId

added in API level 1
int getCheckedRadioButtonId ()

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

Related XML Attributes:

Returns
int the unique id of the selected radio button in this group

See also:

onProvideAutofillStructure

added in API level 26
void onProvideAutofillStructure (ViewStructure structure, 
                int flags)

Populates a ViewStructure to fullfil an autofill request.

The structure should contain at least the following properties:

It's also recommended to set the following properties - the more properties the structure has, the higher the changes of an AutofillService properly using the structure:

The default implementation of this method already sets most of these properties based on related View methods (for example, the autofill id is set using getAutofillId(), the autofill type set using getAutofillType(), etc.), and views in the standard Android widgets library also override it to set their relevant properties (for example, TextView already sets the text properties), so it's recommended to only override this method (and call super.onProvideAutofillStructure()) when:

NOTE: the left and top values set in setDimens(int, int, int, int, int, int) must be relative to the next isImportantForAutofill() predecessor view included in the structure.

Views support the Autofill Framework mainly by:

  • Providing the metadata defining what the view means and how it can be autofilled.
  • Notifying the Android System when the view value changed by calling notifyValueChanged(View).
  • Implementing the methods that autofill the view.

This method is responsible for the former; autofill(AutofillValue) is responsible for the latter.

Parameters
structure ViewStructure: fill in with structured view data for autofill purposes.

flags int: optional flags.

setOnCheckedChangeListener

added in API level 1
void setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)

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

Parameters
listener RadioGroup.OnCheckedChangeListener: the callback to call on checked state change

setOnHierarchyChangeListener

added in API level 1
void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

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

Parameters
listener ViewGroup.OnHierarchyChangeListener: the callback to invoke on hierarchy change

Protected methods

checkLayoutParams

added in API level 1
boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateDefaultLayoutParams

added in API level 1
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. When the orientation is HORIZONTAL, the width is set to WRAP_CONTENT and the height to WRAP_CONTENT.

Returns
LinearLayout.LayoutParams a set of default layout parameters or null

onFinishInflate

added in API level 1
void onFinishInflate ()

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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.