Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't deselect buttons using a custom layout
    primarykey
    data
    text
    <p>I'm implementing a container with 3 buttons, added dynamically. When I select one, I want that the other ones are deselected (change drawable). This works when I add the buttons directly to a LinearLayout, which I declared in XML. But when I create a custom layout, which extends LinearLayout, and put my buttons there, it doesn't work anymore (although the code is almost the same). I select one button and the code to deselect the other ones is executed, but they still show the drawable of selected state.</p> <p>Edit: I always can select the buttons, in both cases. Selection works. What doesn't work in the custom view case is <strong>de</strong> -selecting the views, or at least the drawable is not changed when the views are deselected.</p> <p>I reduced the code to the minimal necessary to see the problem.</p> <p>The activity: </p> <pre><code>public class TestActivity extends Activity { private View[] views; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); ///////////////////////////////////////////////////////////////// //uncomment this block to see the working code (without custom layout) // views = new View[3]; // LinearLayout root = (LinearLayout) findViewById(R.id.container); // findViewById(R.id.myCustomLayout).setVisibility(View.GONE); // addButton(root, 0); // addButton(root, 1); // addButton(root, 2); ////////////////////////////////////////////////////////////////// } private void addButton(final ViewGroup root, final int position) { View v = new View(this); LinearLayout.LayoutParams viewLayoutPars = new LinearLayout.LayoutParams(150, 200); v.setLayoutParams(viewLayoutPars); v.setBackgroundResource(R.drawable.mybg); if (position == 0) { v.setSelected(true); } root.addView(v); views[position] = v; v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { for (View view : views) { view.setSelected(false); } v.setSelected(true); root.invalidate(); } }); } </code></pre> <p>}</p> <p>The layout file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;com.test.MyCustomLayout android:id="@+id/myCustomLayout" xmlns:dg="http://schemas.android.com/apk/res/com.test" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>My custom layout:</p> <pre><code>public class MyCustomLayout extends LinearLayout { private Context context; private View[] views; public MyCustomLayout(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; views = new View[3]; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); addButton(0); addButton(1); addButton(2); } private void addButton(final int position) { View v = new View(context); LinearLayout.LayoutParams viewLayoutPars = new LinearLayout.LayoutParams(150, 200); v.setLayoutParams(viewLayoutPars); v.setBackgroundResource(R.drawable.mybg); if (position == 0) { v.setSelected(true); } addView(v); views[position] = v; v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { for (View view : views) { view.setSelected(false); } v.setSelected(true); invalidate(); } }); } } </code></pre> <p>The drawable (mybg):</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_selected="true" android:drawable="@drawable/selected" /&gt; &lt;item android:drawable="@drawable/not_selected" /&gt; &lt;/selector&gt; </code></pre> <p>The images are 2 harmless squares selected.png and not_selected.png with different color.</p> <p>Thanks in advance.</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.
 

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