Bug 27766 - On Android, button background is not updated when color changes
Summary: On Android, button background is not updated when color changes
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.5
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-03-09 06:49 UTC by Peter Major
Modified: 2015-04-23 12:06 UTC (History)
7 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample project (179.98 KB, application/zip)
2015-03-09 06:51 UTC, Peter Major
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Peter Major 2015-03-09 06:49:17 UTC
See forum post here:
http://forums.xamarin.com/discussion/30358/on-android-button-background-is-not-updated-in-very-specific-case-xf-1-3

I've found an issue where triggers on a button style are not re-evaluating when a value changes if the button is in a listview and an item is deleted from the listview.

This occurs on Android only.

Here's a cut down sample of my style:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="BackgroundColor" Value="#F2F2F2" /><!-- gray -->
    <Setter Property="Text" Value="A" />
    <Style.Triggers>
        <Trigger Property="controls:ButtonExtensions.IsPrimary" Value="True" TargetType="Button">
            <Setter Property="BackgroundColor" Value="#FAC54A" /> <!-- orange -->
            <Setter Property="Text" Value="B" />
        </Trigger>
    </Style.Triggers>
</Style>
These buttons are in ViewCell's within a ListView.
The ListView is bound to an ObservableCollection
The collection contains 2 models, the first item has "IsPrimary=false" and the second item has "IsPrimary=true"
Delete the first item from the collection
Expected result:
The list contains one item. The button has "Text=A" and a color of orange.

Actual result
The list contains one item. The button has "Text=A" but the color is gray.

You can see that the trigger is firing, because the text is updated. But the background is not updated.

I've got a workaround by copying the existing ButtonRenderer and changing the following line of code in the OnElementPropertyChanged method of the Android renderer:

before:
if (this.drawableEnabled && (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || e.PropertyName == Button.BorderColorProperty.PropertyName || e.PropertyName == Button.BorderRadiusProperty.PropertyName || e.PropertyName == Button.BorderWidthProperty.PropertyName)) {
    this.backgroundDrawable.Reset ();
    base.Control.Invalidate ();
}
after:
if (this.drawableEnabled && (e.PropertyName == "Renderer" || e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || e.PropertyName == Button.BorderColorProperty.PropertyName || e.PropertyName == Button.BorderRadiusProperty.PropertyName || e.PropertyName == Button.BorderWidthProperty.PropertyName)) {
    this.backgroundDrawable.Reset ();
    base.Control.Invalidate ();
}
So when the button is redrawn after ListView item is deleted, this causes the background drawable resource to be recalculated... Don't know if this is a "proper" fix or just a hack, but it's the only way I could get it to work...




--------------------------------------------------

In attached project:

To reproduce:
- on the second item, click the button
- the button will turn orange
- on the second item, click the button again
- the item will be removed from the list
- the third item in the list now has an orange button - WHAT?
Comment 1 Peter Major 2015-03-09 06:51:20 UTC
Created attachment 10212 [details]
Sample project
Comment 2 Udham Singh 2015-03-09 13:27:54 UTC
I have checked this issue with the help of steps given in bug and able to reproduce the reported behaviour.

Screencast : http://www.screencast.com/t/LE7LT4EKy

Ide Log : https://gist.github.com/Udham1/9b01b420c8340a9481bf
Adb Logcat : https://gist.github.com/Udham1/17f18773a60661ea685a
Application Output : https://gist.github.com/Udham1/697b4fd9b2e8950160c7

Environment Info : 

Xamarin.Forms : 1.4.0.6340-pre2

=== Xamarin Studio ===

Version 5.9 (build 126)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
Runtime:
	Mono 4.0.0 ((detached/51d34c6)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000028

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.10.0.9 (Business Edition)
Hash: 283a3f2
Branch: master
Build date: 2015-03-05 01:59:36-0500

=== Xamarin.Android ===

Version: 5.1.0.12 (Business Edition)
Android SDK: /Users/xamarin76/Desktop/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.0.0.1 (Business Edition)

=== Build Information ===

Release ID: 509000126
Git revision: 1655605cb3fe05b3d63c8afbf9b2900f2f08771d
Build date: 2015-03-04 06:17:59-05
Xamarin addins: 564d7bb0d7d0bc4440c14e7baf16db2fadb76975

=== Operating System ===

Mac OS X 10.9.5
Darwin Xamarin76s-Mac-mini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 3 Rui Marinho 2015-04-20 05:28:03 UTC
Should be fixed in 1.4.3-pre1
Comment 4 Parmendra Kumar 2015-04-23 12:06:06 UTC
I have checked this issue with Xamarin.Forms 1.4.3-pre1 and its working fine at my end.

Hence closing this issue.

Environment info:

=== Xamarin Studio ===

Version 5.9 (build 434)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
Runtime:
	Mono 4.0.0 ((detached/fd35b80)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000145

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.iOS ===

Version: 8.10.0.266 (Enterprise Edition)
Hash: c8648f0
Branch: master
Build date: 2015-04-21 12:22:24-0400

=== Xamarin.Android ===

Version: 5.1.0.115 (Enterprise Edition)
Android SDK: /Users/360_macmini/Desktop/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.0.0.266 (Enterprise Edition)

=== Build Information ===

Release ID: 509000434
Git revision: 1291dafc0e3477de3999eb2019517f7dc181e443
Build date: 2015-04-22 18:34:20-04
Xamarin addins: 21fd95f743742c5237e22023996c2800ce3f8db3

=== Operating System ===

Mac OS X 10.9.4
Darwin ShrutiMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64