0
votes

I am writing a button custom renderer for my Xamarin app. Therefore I inherit from the class ButtonRenderer and use the Control to customize my button. I need two images in this button, on the left and on the right. In the middle there is a text label. How do I get a second image in the button in the C#-Class (not in xaml)

1
it would be much easier to do this by creating a custom control in forms with a layout container and a gesture recognizerJason
Could it work ?Leo Zhu - MSFT

1 Answers

0
votes

You could use the SetCompoundDrawablesWithIntrinsicBounds method to add images to the top, bottom, left and right of the Button for Android .

class MyButtonRenderer :ButtonRenderer
{
    public MyButtonRenderer(Context context) : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);
        if (Control !=null)
        {
            Control.SetCompoundDrawablesWithIntrinsicBounds(Resources.GetDrawable(Resource.Drawable.fivePlus), null, Resources.GetDrawable(Resource.Drawable.fivePlus), null);
        }
    }
}