Reputation: 6374
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.
The code that populates the LinearLayout is as follows:
View template = null;
if (items.size() > 0) {
for (int i = 0; i < 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;
}
}
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
This is my view,
And it doesn't matter what item I presse I get this,
This is the item XML:
`
<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" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/custom_list_button"/>
`
The XML on the main layout:
<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" />
</LinearLayout>
Upvotes: 2
Views: 472
Reputation: 6374
The answer was that I can't reuse a drawable object within the list. I need to create a drawable for each of the buttons, that solves the problem. Thanks to raukodraug for all his support, and to the others who point me some issues.
Upvotes: 1
Reputation: 11619
Why dont you try defining template
inside the loop?
if (items.size() > 0) {
for (int i = 0; i < items.size(); i++) {
View 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);
}
}
I hope this helps
EDIT
Try using this for your selector, and also try adding it in the XML.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused state -->
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/button_phone"" />
<!-- Focused state -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/button_phone_over" />
<!-- Pressed state -->
<item android:state_pressed="true"
android:drawable="@drawable/button_phone_over" />
</selector>
I have a feeling that the problem is that you are reusing the drawable in the code, so if you are not doing it in the XML then create a new drawable for each background, instead of reusing the one.
Upvotes: 0
Reputation: 6073
I don't know if this is a bug or a feature but similar behavior can be achieved with following code;
Drawable d = getResources().getDrawable(R.drawable.bg);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setBackgroundDrawable(d);
button2.setBackgroundDrawable(d);
Here button2 is drawn as 'pressed' despite clicking button1 and it seems to relate to fact same drawable is assigned as a background for both buttons. You can get around this by creating own background Drawable for each button.
Upvotes: 0