Class RadioGroup

All Implemented Interfaces:
Drawable.Callback, ViewManager, ViewParent

public class RadioGroup extends LinearLayout

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. So this group only allows a single button to be checked.

Initially, all of the radio buttons are unchecked. If all child views are RadioButton, it is not possible to uncheck a particular radio button, the radio group can be cleared via clearCheck() to remove the checked state. If at least one of the non-radio buttons must be selected, use setSelectionRequired(boolean).

The selection is identified by the unique id of the checkable view as defined by View.setId(int). Therefore, all the checkable children must have a valid unique id. Otherwise this class generates an id for it.

On version 3.12 and above, all Checkable2 subclasses can be added to a radio group and will behave like radio buttons. However it is recommended to add RadioButton (with text) or CheckableImageButton (icon only) to this group, as they are optimized.

On version 3.12 and above, you can nest radio groups by adding them to an outer radio group, and you can add/remove radio buttons or radio groups to any level of radio groups at any time. This class will ensure that the outermost radio group is working. Once the inner radio group is removed from the outer radio group, the inner radio group will start working. For a working radio group, if a button is selected, getCheckedId() is guaranteed to return the unique id of that button, but not necessarily vice versa.

See Also:
  • Constructor Details

    • RadioGroup

      public RadioGroup(Context context)
  • Method Details

    • onViewAdded

      protected void onViewAdded(View child)
      Description copied from class: ViewGroup
      Called when a new child is added to this ViewGroup. Overrides should always call super.onViewAdded.
      Overrides:
      onViewAdded in class ViewGroup
      Parameters:
      child - the added child view
    • onViewRemoved

      protected void onViewRemoved(View child)
      Description copied from class: ViewGroup
      Called when a child view is removed from this ViewGroup. Overrides should always call super.onViewRemoved.
      Overrides:
      onViewRemoved in class ViewGroup
      Parameters:
      child - the removed child view
    • check

      public void check(int id)

      Sets the selection to the radio button whose identifier is passed in parameter. Using View.NO_ID 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
      See Also:
    • getCheckedId

      public final int getCheckedId()

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

      Returns:
      the unique id of the selected radio button in this group
      See Also:
    • clearCheck

      public final void clearCheck()

      Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedId() returns View.NO_ID.

      See Also:
    • setSelectionRequired

      public void setSelectionRequired(boolean selectionRequired)
      Sets whether we prevent all child buttons from being deselected. If a child is not RadioButton, it may be unchecked by user, by setting this to true to prevent it from being unchecked. This is false by default.
    • isSelectionRequired

      public boolean isSelectionRequired()
      Returns whether we prevent all child buttons from being deselected. This is false by default.
      See Also:
    • setOnCheckedChangeListener

      public void setOnCheckedChangeListener(@Nullable RadioGroup.OnCheckedChangeListener listener)

      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
    • generateDefaultLayoutParams

      @NonNull protected ViewGroup.LayoutParams generateDefaultLayoutParams()
      Description copied from class: LinearLayout
      Returns a set of layout parameters with a width of ViewGroup.LayoutParams.MATCH_PARENT and a height of ViewGroup.LayoutParams.WRAP_CONTENT when the layout's orientation is LinearLayout.VERTICAL. When the orientation is LinearLayout.HORIZONTAL, the width is set to ViewGroup.LayoutParams.WRAP_CONTENT and the height to ViewGroup.LayoutParams.WRAP_CONTENT.
      Overrides:
      generateDefaultLayoutParams in class LinearLayout
      Returns:
      a set of default layout parameters