Note that there are some explanatory texts on larger screens.

plurals
  1. PORadioGroup inside HorizontalScrollview, all Radiobutton get selected instead only one
    primarykey
    data
    text
    <p>i'm trying to extend an HorizontalScrollView with a RadioGroup inside, when i populate the radiogroup all the button become selected on click, instead only the one i clicked</p> <pre><code>this is my class: package widgets; import java.util.ArrayList; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; import android.util.AttributeSet; import android.widget.CompoundButton; import android.widget.HorizontalScrollView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class HorizontalListView extends HorizontalScrollView { ArrayList&lt;String&gt; lista = new ArrayList&lt;String&gt;(); ArrayList&lt;RadioButton&gt; listaButton = new ArrayList&lt;RadioButton&gt;(); int selected = -1; Drawable itemSelector; Context context; android.widget.CompoundButton.OnCheckedChangeListener listener; RadioGroup group; public HorizontalListView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initView(); } public HorizontalListView(Context context, ArrayList&lt;String&gt; stringhe, Drawable itemBackground) { super(context); this.context = context; this.lista = stringhe; this.itemSelector = itemBackground; initView(); } @SuppressLint("NewApi") private synchronized void initView() { this.removeAllViews(); group = new RadioGroup(getContext()); group.setOrientation(RadioGroup.HORIZONTAL); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); this.addView(group, params); int i = 0; for (String string : lista) { RadioButton button = new RadioButton(getContext()); if (android.os.Build.VERSION.SDK_INT &lt; android.os.Build.VERSION_CODES.JELLY_BEAN) { button.setBackgroundDrawable(itemSelector); } else { button.setBackground(itemSelector); } button.setButtonDrawable(new StateListDrawable()); button.setText(string); button.setTag(i); LayoutParams paramsButton = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); button.setPadding(20, 0, 20, 0); listaButton.add(button); group.addView(button, paramsButton); i++; } } </code></pre> <p>and this is my selector, i made all the state i need to select the correct one, but as i said the radiogroup select all the items (they also become selected if i scroll the horizontalscrollview)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_checked="true" android:state_pressed="false"&gt;&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;stroke android:width="1dp" android:color="@color/sinottica_button_border" /&gt; &lt;solid android:color="@color/sinottica_button_bg_selected" /&gt; &lt;padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" /&gt; &lt;/shape&gt;&lt;/item&gt; &lt;item android:state_checked="false" android:state_pressed="false"&gt;&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;stroke android:width="1dp" android:color="@color/sinottica_button_border" /&gt; &lt;solid android:color="@color/sinottica_button_bg" /&gt; &lt;padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" /&gt; &lt;/shape&gt;&lt;/item&gt; &lt;item android:state_checked="true" android:state_pressed="true"&gt;&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;stroke android:width="1dp" android:color="@color/sinottica_button_border" /&gt; &lt;solid android:color="@color/sinottica_button_bg_selected" /&gt; &lt;padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" /&gt; &lt;/shape&gt;&lt;/item&gt; &lt;item android:state_checked="false" android:state_pressed="true"&gt;&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;stroke android:width="1dp" android:color="@color/sinottica_button_border" /&gt; &lt;solid android:color="@color/sinottica_button_bg" /&gt; &lt;padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" /&gt; &lt;/shape&gt;&lt;/item&gt; &lt;/selector&gt; </code></pre> <p>UPDATE:</p> <p>i tried to not remove the StateListDrawable, so i commented this row </p> <pre><code> button.setButtonDrawable(new StateListDrawable()); </code></pre> <p>now i have the checkbox over my textview, but while the background is out of sync with the selection, (change when i scroll, the other radiobutton in the group change their selection state when i touch one button) the checkmark is correct... there is a way to put the statelist drawable as background for the Button instead of the default (on the left of the button)</p>
    singulars
    1. This table or related slice is empty.
    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.
    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