Xamarin.Forms: customize Material Entry control

Guriy Samarin
Dec 2, 2019 · 2 min read

If you’d like to customize simple Entry — you can read this article.

Let’s use standard customization options: TextColor, PlaceholderColor, BackgroundColor. I created a pair of Material Entries side by side for showcasing colors that can be easily customized.

We will have the following result

For further customization, we are going to use custom renderers (for each platform). You can read about this in detail in Microsoft site. But I will sum up steps for creating customized Material Entry.

I. Create control for customization

a) add MyMaterialEntry.cs file into your shared project

b) define MyMaterialEntry as Entry’s subclass

Customization of the MyMaterialEntry will be made by the custom renderer, so no additional implementation is required

c) add Bindings for customization properties

We will use these properties for changing customized parameters of MyMaterialEntry

II. Create platform-specific renderer

we will need to do the following for each platform

  1. Subclass the MaterialEntryRenderer
  2. Add an ExportRenderer attribute to the custom renderer class

If a custom renderer for one of the platforms isn’t registered, then the default renderer for the control’s base class will be used.

a) create Android renderer

Override the OnElementChanged

b) create iOS renderer

Override the OnElementChanged and the OnElementPropertyChanged

III. Results

Now, we can use MyEntry like this

And we will have the following result:

IV. Possible problems

Be aware that there was a bug: Replacing default Material Visual Renderers fails if the assemblies are scanned in the “wrong” order. It was resolved in Xamarin.Forms 4.2 version, by adding a priority property to HandlerAttribute so you can prioritize which ExportRenderer is the most important. All of our material renderers set to have short.MinValue priority so if a user wants to replace all the default Material Button renderers they can just do [assembly: ExportRenderer(typeof(Button), typeof(DefaultMaterialRenderer), new[] { typeof(VisualMarker.MaterialVisual) })].

V. Resources

Code above available at

Guriy Samarin

Written by

Software developer since 2006. My work is mainly connected with health (development of medical devices and software)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade