Hi,
I'm rendering a custom button and it looks good but the size of the Android element is weird. The custom button is very simple.
In my PCL I have this:
public class ImageButton : Button
{
public ImageButton()
{
BackgroundColor = Color.Transparent;
}
}
In my Android project I have this class:
[assembly: ExportRenderer(typeof(mahana.CustomControls.ImageButton), typeof(ImageButtonRenderer))]
namespace foo.Droid.CustomControls
{
public class ImageButtonRenderer : ButtonRenderer
{
protected override bool DrawChild(Canvas canvas, global::Android.Views.View child, long drawingTime)
{
// canvas.Width/Height and child.Width/Height are OK here - 1020 width and 144 height
return base.DrawChild(canvas, child, drawingTime);
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
// e.NewElement.Width and e.NewElement.Height are both -1
SetBackgroundResource(Resource.Drawable.bg_button_border);
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// widthMeasureSpec and heightMeasureSpec are extremely high, e.g. 10796174218
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
I render this element in a StackPanel without WidthRequest / HeightRequest. The button has a good size and the BackgroundResource from the Droid project also looks good. In DrawChild, both canvas and child have good sizes (1020x144). But the sizes in OnElementChanged() and OnMeasure() seems to be incorrect.
When I tap far outside my button, then the Android Drawable "OnPressed" effect becomes active. So whenever I click anywhere on my screen - the button has a click effect (Ripple effect) even if I don't press the element. So the canvas and child elements seems OK but some measurement for the custom renderer is way off so that it covers the entire screen and listens to all click events, even outside the button.
I hope the problem is clear. Does anyone know why this is happening and how I can prevent it? What can cause this custom renderer to listen for click effects far outside the canvas/child bounds?