Customizing an Entry using EntryRenderer Class in Xamarin


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

ai-Hiring-banner

Xamarin.forms code runs on multiple platforms, each with its filesystem. This means that reading and writing files on each platform are done very easily using the native file API.

If you've ever worked with Xamarin.forms, you've noticed that our entry control (most commonly used control) platform looks very different between platforms (iOS and Android).

What is Entry?

Entry views are used to allow users to type a single line of text. This one line of text can be used for many purposes of entering basic notes, credentials, URLs, and more. This view is a multi-purpose view, meaning that if you need to type text regularly or want to obscure a password, all of this is done through this standalone control. See the below image for your reference.

Screenshot: Normal Entry Control

Customizing an Entry

Xamarin.forms allow a single line of entry control text to be edited. This article demonstrates how to create a custom renderer for access control, allowing developers to rewrite the default original rendering through their platform-specific customization.

Each Xamarin.forms control has an accompanying renderer for each platform that exemplifies native control. When an entry control is rendered through a Xamarin.form application, the entry renders class is instant in iOS, which in turn instances the original UIText field control. On the Android platform, the EntryRenderer class instances an edit text control. On the Universal Windows Platform (UWP), the EntryRenderer class implements a Text box control. For more information about Renderer and Nettie Control classes Xamarin.forms control the map, see renderer base classes and native controls.

The following figure illustrates the relationship between entry control and the relative control that applies to it:

Reference: https://docs.microsoft.com/

The rendering process can be used by implementing platform-specific personalization by creating a customized rendering engine for input control on each platform.

Creating the Custom Renderer on each Platform


Custom Renderers

Xamarin.forms user interfaces are rendered using the native control of the target platform, which allows Xamarin.forms applications to maintain a proper appearance look for every platform. Custom renderers allow developers to override this process to customize the look and behavior of Xamarin.form Controls on each platform.

The process of creating a custom renderer class is as follows

  1. Create a sub-class of the EntryRenderer class which restores native control.
  2. Override the OnElementChanged A method that makes native control and writing logic for customizing control. This method is invoked during the creation of the corresponding Xamarin.Forms command.
  3. Add an ExportRenderer attribute to the custom renderer class to specify that it will be used to render the Xamarin.form control. This attribute is used to record the custom rendition engine with Xamarin.forms.

Note: Providing a custom renderer is optional in each platform project. If a custom renderer is not registered, then the default renderer will be used for the base class of the control.

The following figure shows the responsibilities of each project in the sample application and the relationship between them:

Reference: https://docs.microsoft.com/

Myentry control is rendered by platform-specific MyEntryRenderer classes, derived from the Entryrenderer class for each platform. As shown in the screenshots below, the results of each Myentry control are presented with a platform-specific background color:

Reference: https://docs.microsoft.com/

The Entryrenderer class discloses the OnElementChanged method, called when the Xamarin.forms control is created to make native controls compatible. This method uses ElementChangeventEventsArgs which has both OldElement and NewElement properties. The properties Xamarin.forms represent the element that was attached to the renderer and Xamarin.forms element that is attached to the render, respectively. The sample application will have the OldElement property null and the NewElement property will have a reference to the Myentry control.

An override version of the OnElementChanged Change method in MyEntryRenderer class is the place to do basic control customization. The typed reference to the native control used on the platform can be accessed through the control property. In addition, the reference to Xamarin.forms control that is being rendered can be obtained through the Element property, although it is not used in the sample application.

Every custom rendering class is equipped with an ExportRenderer attribute which records the rendering with Xamarin.forms. The feature takes two parameters - the name of the type of Xamarin.forms control rendering, and the type name of the custom render. The assembly prefix of the feature specifies that the feature applies to the entire assembly.

The following sections discuss the implementation of each platform-specific Minty Renderer custom renderer class.


Creating the Custom Renderer on iOS

The following code example show the custom renderer for iOS platform:

using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.iOS
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs e)
        {
            base.OnElementChanged (e);

            if (Control != null) {
                // UITextField here!
                Control.BackgroundColor = UIColor.FromRGB (200, 143, 221);
                Control.BorderStyle = UITextBorderStyle.Line;
            }
        }
    }
}

In addition to the band-class OnElementChanged method, iOS instances UITextField control, about the control assigned to the render's control property. The background color is then set to a lighter purple by the UIColor.FromRGB method.

Creating the Custom Renderer on Android

using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.Android
{
    class MyEntryRenderer : EntryRenderer
    {
        public MyEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
            }
        }
    }
}

The following sample code demonstrates a custom rendering engine for the Android platform.

The band instances the Android edit text control over the band-class element change method, regarding the control given to the render's control property. The background color control is then set to a lighter green by the setback color method.

Creating custom Renderer on UWP

The example code below shows the custom rendering engine for UWP:

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.UWP
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.Background = new SolidColorBrush(Colors.Cyan);
            }
        }
    }
}

The call on the band class's OnElementChanged method instances the textbox control, concerning the control assigned to the render's control property. Then the background color is set to blue by creating a solid color brush instance.

Conclusion

This article showed how to create a custom control rendering engine for Xamarin.forms Entry control, allowing developers to overwrite the default native rendering with their platform-specific rendering. Custom renderings provide a powerful approach to customize the appearance of the Xamarin.form. Hope you have understood how to create a Customizing entry in Xamarin.Forms.

ai-Hiring-banner

Xamarin.forms code runs on multiple platforms, each with its filesystem. This means that reading and writing files on each platform are done very easily using the native file API.

If you've ever worked with Xamarin.forms, you've noticed that our entry control (most commonly used control) platform looks very different between platforms (iOS and Android).

What is Entry?

Entry views are used to allow users to type a single line of text. This one line of text can be used for many purposes of entering basic notes, credentials, URLs, and more. This view is a multi-purpose view, meaning that if you need to type text regularly or want to obscure a password, all of this is done through this standalone control. See the below image for your reference.

Screenshot: Normal Entry Control

Customizing an Entry

Xamarin.forms allow a single line of entry control text to be edited. This article demonstrates how to create a custom renderer for access control, allowing developers to rewrite the default original rendering through their platform-specific customization.

Each Xamarin.forms control has an accompanying renderer for each platform that exemplifies native control. When an entry control is rendered through a Xamarin.form application, the entry renders class is instant in iOS, which in turn instances the original UIText field control. On the Android platform, the EntryRenderer class instances an edit text control. On the Universal Windows Platform (UWP), the EntryRenderer class implements a Text box control. For more information about Renderer and Nettie Control classes Xamarin.forms control the map, see renderer base classes and native controls.

The following figure illustrates the relationship between entry control and the relative control that applies to it:

Reference: https://docs.microsoft.com/

The rendering process can be used by implementing platform-specific personalization by creating a customized rendering engine for input control on each platform.

Creating the Custom Renderer on each Platform


Custom Renderers

Xamarin.forms user interfaces are rendered using the native control of the target platform, which allows Xamarin.forms applications to maintain a proper appearance look for every platform. Custom renderers allow developers to override this process to customize the look and behavior of Xamarin.form Controls on each platform.

The process of creating a custom renderer class is as follows

  1. Create a sub-class of the EntryRenderer class which restores native control.
  2. Override the OnElementChanged A method that makes native control and writing logic for customizing control. This method is invoked during the creation of the corresponding Xamarin.Forms command.
  3. Add an ExportRenderer attribute to the custom renderer class to specify that it will be used to render the Xamarin.form control. This attribute is used to record the custom rendition engine with Xamarin.forms.

Note: Providing a custom renderer is optional in each platform project. If a custom renderer is not registered, then the default renderer will be used for the base class of the control.

The following figure shows the responsibilities of each project in the sample application and the relationship between them:

Reference: https://docs.microsoft.com/

Myentry control is rendered by platform-specific MyEntryRenderer classes, derived from the Entryrenderer class for each platform. As shown in the screenshots below, the results of each Myentry control are presented with a platform-specific background color:

Reference: https://docs.microsoft.com/

The Entryrenderer class discloses the OnElementChanged method, called when the Xamarin.forms control is created to make native controls compatible. This method uses ElementChangeventEventsArgs which has both OldElement and NewElement properties. The properties Xamarin.forms represent the element that was attached to the renderer and Xamarin.forms element that is attached to the render, respectively. The sample application will have the OldElement property null and the NewElement property will have a reference to the Myentry control.

An override version of the OnElementChanged Change method in MyEntryRenderer class is the place to do basic control customization. The typed reference to the native control used on the platform can be accessed through the control property. In addition, the reference to Xamarin.forms control that is being rendered can be obtained through the Element property, although it is not used in the sample application.

Every custom rendering class is equipped with an ExportRenderer attribute which records the rendering with Xamarin.forms. The feature takes two parameters - the name of the type of Xamarin.forms control rendering, and the type name of the custom render. The assembly prefix of the feature specifies that the feature applies to the entire assembly.

The following sections discuss the implementation of each platform-specific Minty Renderer custom renderer class.


Creating the Custom Renderer on iOS

The following code example show the custom renderer for iOS platform:

using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.iOS
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs e)
        {
            base.OnElementChanged (e);

            if (Control != null) {
                // UITextField here!
                Control.BackgroundColor = UIColor.FromRGB (200, 143, 221);
                Control.BorderStyle = UITextBorderStyle.Line;
            }
        }
    }
}

In addition to the band-class OnElementChanged method, iOS instances UITextField control, about the control assigned to the render's control property. The background color is then set to a lighter purple by the UIColor.FromRGB method.

Creating the Custom Renderer on Android

using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.Android
{
    class MyEntryRenderer : EntryRenderer
    {
        public MyEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
            }
        }
    }
}

The following sample code demonstrates a custom rendering engine for the Android platform.

The band instances the Android edit text control over the band-class element change method, regarding the control given to the render's control property. The background color control is then set to a lighter green by the setback color method.

Creating custom Renderer on UWP

The example code below shows the custom rendering engine for UWP:

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.UWP
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.Background = new SolidColorBrush(Colors.Cyan);
            }
        }
    }
}

The call on the band class's OnElementChanged method instances the textbox control, concerning the control assigned to the render's control property. Then the background color is set to blue by creating a solid color brush instance.

Conclusion

This article showed how to create a custom control rendering engine for Xamarin.forms Entry control, allowing developers to overwrite the default native rendering with their platform-specific rendering. Custom renderings provide a powerful approach to customize the appearance of the Xamarin.form. Hope you have understood how to create a Customizing entry in Xamarin.Forms.