Declined
Last Updated: 14 May 2019 14:04 by ADMIN
Marc
Created on: 06 Feb 2019 15:00
Category: Button
Type: Bug Report
4
RadButton: [Android] BackgroundColor cannot be changed from Renderer.
RadButton Background color cannot be changed through Custom Renderer
4 comments
ADMIN
Pavel R. Pavlov
Posted on: 14 May 2019 14:04
Hello Mark and Ben,

I investigated this case and I have some news to share with you.

The XamarinForms part of the RadButton exposes BackgroundImage property. It allows users to set an ImageSource object as background. This feature brings specific limitations with it, however. In particular, loading images should be asynchronous to avoid freezing the entire UI of the application hosting the RadButton.
On the other hand, the image is applied to the background of the underlying android view. Also, this default background object is responsible for visualizing several features of the component - BorderThickness, BorderColor, CornerRadius, BackgroundColor and BackgroundImage.

Having these two specifics in mind, we have no other option than to create the entire background object asynchronously in another thread. This is the reason why setting the same properties synchronously in a custom renderer does not appear to be working. The truth is that the synchronously set property values get overridden by the asynchronous logic.

This means that when a custom renderer is used, you have to take into account this async code. What you can do is to set the XamarinForms properties instead of the respective native ones. In particular, you can try using this code:

public class CustomButtonRenderer : Telerik.XamarinForms.InputRenderer.Android.ButtonRenderer
{
    public CustomButtonRenderer(Context context) :
        base(context)
    {
    }
 
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);
        this.ModifyBackgroundColor();
    }
 
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (e.PropertyName == Xamarin.Forms.Button.IsEnabledProperty.PropertyName)
        {
            this.ModifyBackgroundColor();
        }
    }
 
    private void ModifyBackgroundColor()
    {
        if (this.Element.IsEnabled)
        {
            this.Element.BackgroundColor = Color.Red;
        }
        else
        {
            this.Element.BackgroundColor = Color.Gray;
        }
    }
}

This code relies on the fact that the synchronous logic finish executing before the asynchronous does and when the async code is triggered, the already updated XamarinForms properties will be applied to the underlying native component.

So, basically, the initially logged issue is not a bug in the control, it's a result of the implementation of applying the background. Taking this into account, I am going to update the status of this item to "Declined".

Let me know if you have any additional questions or concerns regarding this.

Regards,
Pavel R. Pavlov
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Ben
Posted on: 15 Feb 2019 17:28
Got it, thank you Didi!
ADMIN
Didi
Posted on: 15 Feb 2019 14:14
Hello Ben,

Currently the workaround I could suggest is to change the RadButton BackgroundColor with the following condition:

private void SetButtonBackgroundColor()
{
    if (this.btn.IsEnabled == true)
    {
        this.btn.BackgroundColor = Color.Red;
    }
    else
    {
        this.btn.BackgroundColor = Color.Green;
    }
}

Regards,
Didi
Progress Telerik
Ben
Posted on: 14 Feb 2019 15:42
Is there a work-around for this until it is fixed?