Center image and text on button

AzucenaFAzucenaF MXMember ✭✭
edited October 2015 in Xamarin.Forms

Is there a way to center the text and image on a button? I want the image to be on top and text below, both centered on button.
I'm trying to use a custom renderer on android like this

class MenuButtonRenderer: ButtonRenderer
{   
    protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged (e);
        if (e.OldElement == null) {   // perform initial setup
            var nativeButton = (global::Android.Widget.Button)Control;
            nativeButton.Gravity = (global::Android.Views.GravityFlags.CenterHorizontal | global::Android.Views.GravityFlags.CenterVertical);
            nativeButton.SetPadding (20, 30, 0, 0);
        }
    }
}

Answers

  • rzee7rzee7 INUniversity ✭✭✭✭✭

    Hi @AzucenaF

    I'm not sure, but It might help you in this scenario take a look below code snippet:

    class MenuButtonRenderer: ButtonRenderer
    {   
        protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged (e);
            if (Control != null) {   // perform initial setup
    
                Control.Gravity = global::Android.Views.GravityFlags.Center;
                Control.SetPadding (20, 30, 0, 0);
    
                //Image on Top
                Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, Resource.Drawable.YourImage, 0, 0);
            }
        }
    }
    

    Cheers!!
    RIYAZ

  • ahmedElsayedahmedElsayed USMember ✭✭

    @AzucenaF i wish you are fine ,i struggling with the same issue did you fin an answer for that?

Sign In or Register to comment.