Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Xamarin.Forms — Placeholder in Editor Using Custom Renderer

DZone's Guide to

Xamarin.Forms — Placeholder in Editor Using Custom Renderer

Learn how to use custom renderers in Xamarin.Forms development to customize the look and feel of your app.

· Mobile Zone ·
Free Resource

Introduction

Image title


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 data files within 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. Check out the documentation for more information.

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.

Image title

Now select the Blank App and Choose Portable Class Library (PCL).

Subsequently, go to the solution. There, you'll 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.

Create a Default Editor

In this step, create a default Editor control with border. As you can see, there is no placeholder property.

Now, write the following code:

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinFormsEditor"
             x:Class="XamarinFormsEditor.MainPage">
    <ContentPage.Content>
        <StackLayout>
            <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">
                <Image  x:Name="imgBanner" HeightRequest="300" WidthRequest="300"></Image>
                <Editor Text="Enter Some text here..." HeightRequest="100" BackgroundColor="LightGray"></Editor>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>


Click the Play button to try it out.

Image title

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.

Adding a Bindable Property

Add following bindable property to the Editor:

  1. Placeholder

  2. PlaceholderColor

Now, write the following code:

XEditor.cs

using Xamarin.Forms;
namespace XamarinFormsEditor.Controls
{
    public class XEditor:Editor
    {
        public static BindableProperty PlaceholderProperty
            = BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(XEditor));

        public static BindableProperty PlaceholderColorProperty
            = BindableProperty.Create(nameof(PlaceholderColor), typeof(Color), typeof(XEditor), Color.Gray);

        public string Placeholder
        {
            get { return (string)GetValue(PlaceholderProperty); }
            set { SetValue(PlaceholderProperty, value); }
        }

        public Color PlaceholderColor
        {
            get { return (Color)GetValue(PlaceholderColorProperty); }
            set { SetValue(PlaceholderColorProperty, value); }
        }
    }
}


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

Now, write the code given below:

XEditorRenderer.cs

using Xamarin.Forms;
using XamarinFormsEditor.Controls;
using XamarinFormsEditor.Droid.Helpers;
using Xamarin.Forms.Platform.Android;
using Android.Graphics.Drawables;

[assembly: ExportRenderer(typeof(XEditor), typeof(XEditorRenderer))]
namespace XamarinFormsEditor.Droid.Helpers
{
    public class XEditorRenderer: EditorRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                if (Element == null)
                    return;

                var element = (XEditor)Element;
                Control.Hint = element.Placeholder;
                Control.SetHintTextColor(element.PlaceholderColor.ToAndroid());
            }
        }
    }
}


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

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinFormsEditor"
             xmlns:Controls="clr-namespace:XamarinFormsEditor.Controls"
             x:Class="XamarinFormsEditor.MainPage">
    <ContentPage.Content>
        <StackLayout>
            <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">
                <Image  x:Name="imgBanner" HeightRequest="300" WidthRequest="300"></Image>
                <Controls:XEditor Placeholder="Enter Some text here..." HeightRequest="100" BackgroundColor="LightGray"></Controls:XEditor>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>


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

MainPage.Xaml.cs

using Xamarin.Forms;

namespace XamarinFormsEditor
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            imgBanner.Source = ImageSource.FromResource("XamarinFormsEditor.images.banner.png");
        }
    }
}


Click the Play button to try it out.

Image title

I hope you have understood how to Create a Placeholder in Editor Using Custom Renderer.

Thanks for reading. Please share comments and feedback.

Topics:
xamarin ,xamarin.forms ,tutorial ,mobile ,mobile app development ,cross platform development

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}