Articles in this section
Category / Section

How to customize Xamarin.Forms AutoComplete and change the TextAlignemnt to center?

4 mins read

Step 1: Create a custom SfAutoComplete control.

A custom Xamarin.Forms AutoComplete 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.

Conclusion

I hope you enjoyed learning about how to customize SfAutoComplete and change the TextAlignemnt to center?.

You can refer to our  Xamarin.Forms AutoComplete feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Xamarin.Forms AutoComplete example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied