Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tint size not correct in Xamarin Forms with .NETStandard 2.0

I created a new xamarin forms project with .NET Standard 2.0 and in the sample i changed the label to Button and added the background color. While the button is clicked the ripple animation is shown small and concentrated to the top left corner and not the entire button width and height

<Button Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" 
   VerticalOptions="CenterAndExpand" BackgroundColor="Lime"  />

Is there anything we need to do to make the ripple animation correct.

Please see the image attached.

enter image description here

like image 501
George Thomas Avatar asked Jul 31 '18 11:07

George Thomas


1 Answers

If you want to remove the extra tint animation part. Then you have to write custom render class for Button in Android platfrom. Please check the sample code

    assembly: ExportRenderer(typeof(TintableButton), typeof(TintableButtonRenderer))]
namespace XamTest.Droid.Renderers
{
    public class TintableButtonRenderer : ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);

            var control = e.NewElement as TintableButton;
            if (control != null)
            {
                if (control.TintColor != Xamarin.Forms.Color.Default)
                {
                    var androidColor = control.TintColor.ToAndroid();
                    Control.Background.SetColorFilter(androidColor, PorterDuff.Mode.Src);
                }
            }
        }
    }
}

Call this custom control in UI.

 <Grid>
    <controls:TintableButton Text="Test" HorizontalOptions="FillAndExpand" />
   </Grid>
like image 170
Pratius Dubey Avatar answered Sep 18 '22 01:09

Pratius Dubey