Xamarin.Forms - Placeholder In Editor Using Custom Renderer

Xamarin
 
The Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the data files with an app.

Custom Renderers

Xamarin.Forms user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform. Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
 
Choose the Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.
 
Xamarin 

Now Select the Blank App and Choose Portable Class Library(PCL).
 
Xamarin
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (PCL). Now, select XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
Xamarin

In this step, create a default Editor control with border. You can see there is no placeholder property. Now, write the following code.

MainPage.xaml
 
Xamarin
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:XamarinFormsEditor"  
  5.              x:Class="XamarinFormsEditor.MainPage">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.             <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">  
  9.                 <Image  x:Name="imgBanner" HeightRequest="300" WidthRequest="300"></Image>  
  10.                 <Editor Text="Enter Some text here..." HeightRequest="100" BackgroundColor="LightGray"></Editor>  
  11.             </StackLayout>  
  12.         </StackLayout>  
  13.     </ContentPage.Content>  
  14. </ContentPage>  
Click the Play button to try it out.
 
Xamarin
 
Create a Custom Editor

Now, create an Inherit class form Editor for customizing the Editor control.
 
Go to Solution—>PCL—>Right click—>New—>Class—>XEditor.cs.
 
Xamarin 
 
Adding Bindable Property

Add the following Bindable Property to the Editor.
  1. Placeholder
  2. PlaceholderColor
Xamarin
 
Now, write the following code.

XEditor.cs
  1. using Xamarin.Forms;  
  2. namespace XamarinFormsEditor.Controls  
  3. {  
  4.     public class XEditor:Editor  
  5.     {  
  6.         public static BindableProperty PlaceholderProperty  
  7.             = BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(XEditor));  
  8.   
  9.         public static BindableProperty PlaceholderColorProperty  
  10.             = BindableProperty.Create(nameof(PlaceholderColor), typeof(Color), typeof(XEditor), Color.Gray);  
  11.   
  12.         public string Placeholder  
  13.         {  
  14.             get { return (string)GetValue(PlaceholderProperty); }  
  15.             set { SetValue(PlaceholderProperty, value); }  
  16.         }  
  17.   
  18.         public Color PlaceholderColor  
  19.         {  
  20.             get { return (Color)GetValue(PlaceholderColorProperty); }  
  21.             set { SetValue(PlaceholderColorProperty, value); }  
  22.         }  
  23.     }  
  24. }  
Making Your Android Implementation

In this step, create an inherit Class form, EditorRenderer for customizing the Editor control.

Go to Solution.Droid—>Class—> XEditorRenderer.cs.

Xamarin

Now, write the code given below.

XEditorRenderer.cs
  1. using Xamarin.Forms;  
  2. using XamarinFormsEditor.Controls;  
  3. using XamarinFormsEditor.Droid.Helpers;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using Android.Graphics.Drawables;  
  6.   
  7. [assembly: ExportRenderer(typeof(XEditor), typeof(XEditorRenderer))]  
  8. namespace XamarinFormsEditor.Droid.Helpers  
  9. {  
  10.     public class XEditorRenderer: EditorRenderer  
  11.     {  
  12.         protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)  
  13.         {  
  14.             base.OnElementChanged(e);  
  15.             if (Control != null)  
  16.             {  
  17.                 if (Element == null)  
  18.                     return;  
  19.   
  20.                 var element = (XEditor)Element;  
  21.                 Control.Hint = element.Placeholder;  
  22.                 Control.SetHintTextColor(element.PlaceholderColor.ToAndroid());  
  23.             }  
  24.         }  
  25.     }  

 Xamarin
 
Setting up the User Interface

Now, add a customized Editor control to your app. Go to MainPage.Xaml and write the following code.

MainPage.Xaml

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:XamarinFormsEditor"  
  5.              xmlns:Controls="clr-namespace:XamarinFormsEditor.Controls"  
  6.              x:Class="XamarinFormsEditor.MainPage">  
  7.     <ContentPage.Content>  
  8.         <StackLayout>  
  9.             <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">  
  10.                 <Image  x:Name="imgBanner" HeightRequest="300" WidthRequest="300"></Image>  
  11.                 <Controls:XEditor Placeholder="Enter Some text here..." HeightRequest="100" BackgroundColor="LightGray"></Controls:XEditor>  
  12.             </StackLayout>  
  13.         </StackLayout>  
  14.     </ContentPage.Content>  
  15. </ContentPage> 
Xamarin

Go to MainPage.Xaml.cs and write the following code (optional).

MainPage.Xaml.cs
  1. using Xamarin.Forms;  
  2.   
  3. namespace XamarinFormsEditor  
  4. {  
  5.     public partial class MainPage : ContentPage  
  6.     {  
  7.         public MainPage()  
  8.         {  
  9.             InitializeComponent();  
  10.             imgBanner.Source = ImageSource.FromResource("XamarinFormsEditor.images.banner.png");  
  11.         }  
  12.     }  

Xamarin
 
Click the Play button to try it out.
 
Xamarin 

I hope you have understood how to create a Placeholder Editor using Custom Renderer.
 
Thanks for reading. Please share comments and feedback.


Similar Articles