Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected behaviour while clicking a button contained in a custom LinearLayout
    primarykey
    data
    text
    <p>I have a problem with a custom LinearLayout I'm creating for an application. Basically, the LinearLayout contain items, each item is a horizontal LinearLayout which contains a TextView and a Button. The LinearLayout is populated correctly, however, when I press the button (which has a background a custom background: selector) unexpected behaviour is happening. What's happening is that the button changes is appearance only on the last element of the LinearLayout, which doesn't make any sense. </p> <p>The code that populates the LinearLayout is as follows:</p> <pre><code>View template = null; if (items.size() &gt; 0) { for (int i = 0; i &lt; items.size(); i++) { template = inflate(getContext(), R.layout.views_custom_list_item, null); template.setBackgroundDrawable(getProperBackgroundDrawable(i, items.size() - 1)); TextView text = (TextView)template.findViewById(R.id.custom_list_text); text.setText(items.get(i)); Button button = (Button)template.findViewById(R.id.custom_list_button); button.setId(i); button.setBackgroundDrawable(backgroundButton); button.setOnClickListener(callListener); addView(template); template = null; } } </code></pre> <p>I'm inflating an XML which contains the layout for each item, and then I set the properties accordingly. All that code is contained in a custom class which inherits from LinearLayout. Do you know where the problem is? thanks</p> <p>This is my view,</p> <p><img src="https://i.stack.imgur.com/TtLRU.png" alt="enter image description here"></p> <p>And it doesn't matter what item I presse I get this,</p> <p><img src="https://i.stack.imgur.com/pYR2S.png" alt="enter image description here"></p> <p>This is the item XML:</p> <p>`</p> <pre><code>&lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/custom_list_text" android:textSize="15sp" android:textColor="@color/black" android:paddingLeft="5dip" android:paddingRight="5dip" android:layout_weight="1" android:gravity="left" /&gt; &lt;ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/custom_list_button"/&gt; </code></pre> <p>`</p> <p>The XML on the main layout:</p> <p><code>&lt;com.example.views.CustomLayoutView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/custom_phone_directory" android:layout_margin="5dip" xx:background_single="@drawable/button_complete" xx:background_top="@drawable/button_top" xx:background_middle="@drawable/button_middle" xx:background_bottom="@drawable/button_bottom" xx:button_background="@drawable/button_phone_selector" /&gt; &lt;/LinearLayout&gt;</code></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.
 

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