Xamarin.forms: [Android] Image on Button is not centered with text

Created on 9 Aug 2018  路  7Comments  路  Source: xamarin/Xamarin.Forms

Description

The Image is not in the center on Button when displaying with a text.

Steps to Reproduce

  1. Create a Button with an image & text.
  2. Set thee HorizontalOption to Fill&Expand.
  3. You can see that the image is stick to the left border
  4. If you change the position(Top,Right...) of the image it will stick on the position selected, not centered with the text

Expected Behavior

The image have to stick the text on the center.

Actual Behavior

The Image is stick to the border depends of the selected position.
I was searching a way to make a correction on UpdateBitmap
But PropertyName "Image" never pass to UpdateBitmap

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == Button.TextProperty.PropertyName)
                UpdateBitmap();

            base.OnElementPropertyChanged(sender, e);
        }

Basic Information

  • Xamarin.Forms: 3.1.0.697729

    • Android: 8.1

button 3 help wanted inactive Android bug up-for-grabs

All 7 comments

OnElementPropertChanged only fires when you change a property on an Element after it has been rendered on the screen. When are you setting the Image property? If you're setting it in the constructor, that will be handled by OnElementChanged. If you still need assistance with this issue, please reopen it and attach a small reproduction project. Thank you!

First thanks for your time,

Forget about the PropertyChanged (my bad, I was expected ImageProperty not TextProperty in sample)

The issue is about the image stick close to the border, depending of Button.ContentLayout.Position
Android:
android-button

expected like on iOS:
ios-button

The image is not centered and it's caused by:

  • The SetCompoundDrawablesWithIntrinsicBounds function in ButtonRenderer ligne 216
    when switching position.
  • And the default Draw function.

-> Here an implementation in Java of a Custom drawn Android button which aligns left drawable and its text to center.

Regards,

X:\git\forms\Xamarin.Forms.Controls.Issues\Xamarin.Forms.Controls.Issues.Shared\Bugzilla27417.cs

Does anyone have a workaround?

Does anyone have a workaround?

You could use the option of 'padding' on the button itself as a work around?

Does anyone have a workaround?

You could use the option of 'padding' on the button itself as a work around?

Yep, that's what I've ended up with (I'm lucky to have a fixed width buttons in this project). Thanks

So, I have fixed this for Material, and encapsulated all the logic for button layout into a single place: https://github.com/xamarin/Xamarin.Forms/pull/4967

Right now, all that is needed to get the image in either place is a boolean flag. However, as this is a breaking change for the older renderers, we will probably not be able to change the behaviour.

However, there are some things we can do:

  • use material for your buttons
  • create a custom renderer and set that flag (if this is an option, then I can add a hook)
  • create an android specific (which you can set with styles)
Was this page helpful?
0 / 5 - 0 ratings