We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize SfAutoComplete and change the TextAlignemnt to center

Platform: Xamarin.Forms |
Control: SfAutoComplete |
Published Date: December 17, 2018 |
Last Revised Date: June 24, 2019

Step 1: Create a custom SfAutoComplete control.

A custom SfAutoComplete control can be created by sub-classing the SfAutoComplete control as shown in the following codes.

XAML

Mainpage.Xaml
 
<ContentPage.Content>
        <StackLayout Padding="50">
            <local:CustomAutoComplete/>
        </StackLayout>
    </ContentPage.Content>

 

C#

MainPage.Xaml.cs
 
public class CustomAutoComplete : SfAutoComplete 
{
 
}
 

 

The custom SfAutoComplete control is created in .NET Standard library project and is simply an SfAutoComplete control. Customization of the control will be carried out in the custom renderer, so no additional implementation is required in the custom SfAutoComplete control.

 

Step 2: Create a custom renderer in Android.

 

C#

[assembly : ExportRenderer(typeof(CustomAutoComplete),typeof(AutoCompleteRenderer))]
 
namespace Auto_Renderer.Droid
{
   
    public class AutoCompleteRenderer : SfAutoCompleteRenderer
    {   
     protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.GetAutoEditText().Gravity = GravityFlags.CenterHorizontal;
 
            }
        }
 
    }
}
 

 

The call to the base class's OnElementChanged method instantiates an Android SfAutoComplete with a reference to assign the control to the renderer's Control property.

 

The Text Alignment is then set to center with the Control.GetAutoEditText().Gravity.

 

Step 3: Create a custom renderer in iOS.

 

C#

[assembly: ExportRenderer(typeof(CustomAutoComplete), typeof(AutoCompleteRenderer))]
namespace Auto_Renderer.iOS
{
    public class AutoCompleteRenderer : SfAutoCompleteRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.TextField.TextAlignment = UITextAlignment.Center;
            }
        }
 
    }
}

 

 

The call to the base class's OnElementChanged method instantiates an iOS SfAutoComplete with a reference to assign the control to the renderer's Control property. 

 

The Text Alignment is then set to center with the UITextAlignment.Center.

 

 

Step 4: Create a custom renderer in UWP.

 

C#

[assembly: ExportRenderer(typeof(CustomAutoComplete), typeof(AutoCompleteRenderer))]
namespace Auto_Renderer.UWP
{
    public class AutoCompleteRenderer : SfAutoCompleteRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.TextAlignment = Windows.UI.Xaml.TextAlignment.Center;           
            }
        }
    }
}

 

The call to the base class's OnElementChanged method instantiates a SfAutoComplete control with a reference to assign the control to the renderer's Control property.

 

The Text Alignment is then set to center with the Windows.UI.Xaml.TextAlignment.Center.

You can download the entire source code of this demo.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon