Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding custom view with radio button to radio group
    primarykey
    data
    text
    <p>I have a custom view that holds, among other views, a RadioButton.</p> <p>The SingleRadioItem:</p> <pre><code>public class SingleRadioItem extends LinearLayout { private TextView mTextKey; private RadioButton mRadioButton; private ImageView mImageSeparator; public SingleRadioItem(Context context, AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.rtl_single_radio_item, this, true); mTextKey = (TextView)view.findViewById(R.id.single_radio_item_text_key); mRadioButton = (RadioButton)view.findViewById(R.id.single_radio_item_button); mImageSeparator = (ImageView)view.findViewById(R.id.single_image_separator); } public void setKey(String key) { mTextKey.setText(key); } public boolean getSelectedState() { return mRadioButton.isSelected(); } public void setSelectedState(boolean selected) { mRadioButton.setSelected(selected); } } </code></pre> <p>I want to create instances of this view, add them to a RadioGroup and add the RadioGroup to a LinearLayout. When I do so, it allows me to set all the radio buttons as selected, which means, the RadioGroup isn't functioning well (probably because how I do it..)</p> <pre><code>RadioGroup radioGroup = new RadioGroup(this); radioGroup.setOrientation(RadioGroup.VERTICAL); SingleRadioItem radio1 = new SingleRadioItem(this, null); SingleRadioItem radio2 = new SingleRadioItem(this, null); radioGroup.addView(radio1); radioGroup.addView(radio2); updateDetailsView.addView(radioGroup); </code></pre> <p>Obviously, when I add <code>RadioButton radio1 = new RadioButton(this);</code> the RadioGroup works well.</p> <p>Is it even possible to add a view that holds a radio button to a radiogroup and I'm just missing something or not possible at all?</p> <p>Thanks!</p> <p><strong>SOLUTION:</strong> To extend @cosmincalistru Answer and help others:</p> <p>for each SingleRadioItem I added to the LinearLayout I attached a listener like this:</p> <pre><code>radio1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (lastRadioChecked != null) { lastRadioChecked.setCheckedState(false); } lastRadioChecked = (SingleRadioItem)v; lastRadioChecked.setCheckedState(true); } }); </code></pre> <p>You also need to set the RadioButton View inside the SingleRadioItem XML to clickable:false.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload