[Xamarin Forms] Button text alignment issue in Android


Recently, I had to deal with custom Buttons in my Android application.
So I designed custom renderers to render it.

Unfortunately, I encountered a strange behavior with the text alignment of my buttons.
This happens when the button changes of state:

- Originally the button's text is centered
- When you click it, the text moves to the left

See images below:

 
Before / after the button click


After investigating, I found a solution to my problem.
In my Android custom button renderer, I had to overwrite a specific method: ChildDrawableStateChanged


public class ExtButtonRenderer : ViewRenderer<Extbutton, global::android.widget.button> 
{
    ...
   
    public override void ChildDrawableStateChanged(Android.Views.View child)
    {
        base.ChildDrawableStateChanged(child);

        if (Control != null)
            Control.Text = Control.Text;
    }
    ...
}


That's all !



Related links:

Custom renderers:
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/


Djooleean

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 commentaires: