FileDocCategorySizeDatePackage
RadioGroup.javaAPI DocAndroid 5.1 API13248Thu Mar 12 22:22:10 GMT 2015android.widget

RadioGroup

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.

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 {@link android.R.styleable#RadioGroup RadioGroup Attributes}, {@link android.R.styleable#LinearLayout LinearLayout Attributes}, {@link android.R.styleable#ViewGroup ViewGroup Attributes}, {@link android.R.styleable#View View Attributes}

Also see {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams} for layout attributes.

see
RadioButton

Fields Summary
private int
mCheckedId
private CompoundButton.OnCheckedChangeListener
mChildOnCheckedChangeListener
private boolean
mProtectFromCheckedChange
private OnCheckedChangeListener
mOnCheckedChangeListener
private PassThroughHierarchyChangeListener
mPassThroughListener
Constructors Summary
public RadioGroup(android.content.Context context)
{@inheritDoc}


          
       
        super(context);
        setOrientation(VERTICAL);
        init();
    
public RadioGroup(android.content.Context context, android.util.AttributeSet attrs)
{@inheritDoc}

        super(context, attrs);

        // retrieve selected radio button as requested by the user in the
        // XML layout file
        TypedArray attributes = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.RadioGroup, com.android.internal.R.attr.radioButtonStyle, 0);

        int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton, View.NO_ID);
        if (value != View.NO_ID) {
            mCheckedId = value;
        }

        final int index = attributes.getInt(com.android.internal.R.styleable.RadioGroup_orientation, VERTICAL);
        setOrientation(index);

        attributes.recycle();
        init();
    
Methods Summary
public voidaddView(android.view.View child, int index, ViewGroup.LayoutParams params)

        if (child instanceof RadioButton) {
            final RadioButton button = (RadioButton) child;
            if (button.isChecked()) {
                mProtectFromCheckedChange = true;
                if (mCheckedId != -1) {
                    setCheckedStateForView(mCheckedId, false);
                }
                mProtectFromCheckedChange = false;
                setCheckedId(button.getId());
            }
        }

        super.addView(child, index, params);
    
public voidcheck(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 {@link #clearCheck()}.

param
id the unique id of the radio button to select in this group
see
#getCheckedRadioButtonId()
see
#clearCheck()

        // don't even bother
        if (id != -1 && (id == mCheckedId)) {
            return;
        }

        if (mCheckedId != -1) {
            setCheckedStateForView(mCheckedId, false);
        }

        if (id != -1) {
            setCheckedStateForView(id, true);
        }

        setCheckedId(id);
    
protected booleancheckLayoutParams(ViewGroup.LayoutParams p)
{@inheritDoc}

        return p instanceof RadioGroup.LayoutParams;
    
public voidclearCheck()

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

see
#check(int)
see
#getCheckedRadioButtonId()

        check(-1);
    
protected LinearLayout.LayoutParamsgenerateDefaultLayoutParams()

        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
public android.widget.RadioGroup$LayoutParamsgenerateLayoutParams(android.util.AttributeSet attrs)
{@inheritDoc}

        return new RadioGroup.LayoutParams(getContext(), attrs);
    
public intgetCheckedRadioButtonId()

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

return
the unique id of the selected radio button in this group
see
#check(int)
see
#clearCheck()
attr
ref android.R.styleable#RadioGroup_checkedButton

        return mCheckedId;
    
private voidinit()

        mChildOnCheckedChangeListener = new CheckedStateTracker();
        mPassThroughListener = new PassThroughHierarchyChangeListener();
        super.setOnHierarchyChangeListener(mPassThroughListener);
    
protected voidonFinishInflate()
{@inheritDoc}

        super.onFinishInflate();

        // checks the appropriate radio button as requested in the XML file
        if (mCheckedId != -1) {
            mProtectFromCheckedChange = true;
            setCheckedStateForView(mCheckedId, true);
            mProtectFromCheckedChange = false;
            setCheckedId(mCheckedId);
        }
    
public voidonInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)

        super.onInitializeAccessibilityEvent(event);
        event.setClassName(RadioGroup.class.getName());
    
public voidonInitializeAccessibilityNodeInfo(android.view.accessibility.AccessibilityNodeInfo info)

        super.onInitializeAccessibilityNodeInfo(info);
        info.setClassName(RadioGroup.class.getName());
    
private voidsetCheckedId(int id)

        mCheckedId = id;
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
        }
    
private voidsetCheckedStateForView(int viewId, boolean checked)

        View checkedView = findViewById(viewId);
        if (checkedView != null && checkedView instanceof RadioButton) {
            ((RadioButton) checkedView).setChecked(checked);
        }
    
public voidsetOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener listener)

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

param
listener the callback to call on checked state change

        mOnCheckedChangeListener = listener;
    
public voidsetOnHierarchyChangeListener(OnHierarchyChangeListener listener)
{@inheritDoc}

        // the user listener is delegated to our pass-through listener
        mPassThroughListener.mOnHierarchyChangeListener = listener;