[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/


Comments

  1. Thank you for taking the time to write such an informative post. Your blog is not only informative, but it is also very creative.

    Android app design company

    ReplyDelete

Post a Comment

Popular posts from this blog

EMGU with Xamarin Forms guide [part 1]

Xamarin Forms essential: FFImageLoading, manage your images

[SocialCut] released! Make instagram grids, panorama and more