Adding Placeholder to Editor

As we know, in Xamarin, Editor, just like Entry, is UI component where user can type the input they want. Unlike Entry, Editor supports multilines input, but as a downside, it doesn’t have placeholder, a hint text about what user should input. But sometime in projects, we need multilines user entry that has placeholder. And to do that, we need some kind of custom Editor that accommodate placeholder as one if its properties. How to do that is what we’ll discuss in this post.

Custom Editor

In Entry, we have properties like Placeholder and Placeholder color. So we need to add those two properties in our Custom Editor. And of  course, it should be bindable as well. The following code is the example of the Custom Editor class.

public class CustomEditor : Editor
{
    public static BindableProperty PlaceholderProperty =
        BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(CustomEditor), string.Empty, BindingMode.TwoWay);
    public string Placeholder
    {
        get { return (string)GetValue(PlaceholderProperty); }
        set { SetValue(PlaceholderProperty, value); }
    }

    public static BindableProperty PlaceholderColorProperty =
        BindableProperty.Create(nameof(PlaceholderColor), typeof(string), typeof(CustomEditor), "#cccccc", BindingMode.TwoWay);

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

In that example, the default value of the placeholder will be empty string, and the default color of the placeholder will be color with hex code #cccccc, it’s just a gray color I picked randomly. For the next step, we will manage how the editor will behave.

Editor Behavior

The second step, we’ll create a behavior class for our custom editor. This class’s task is mainly manage when the placeholder will appear and when it will be gone. Because originally Editor doesn’t have a place to show a placeholder, so we will use Text property of the Editor, to show  up our placeholder.

public class EditorBehavior : Behavior<CustomEditor>
{
    protected override void OnAttachedTo(CustomEditor editor)
    {
        editor.BindingContextChanged += OnBindingContextChanged;
        editor.Focused += OnEditorFocused;
        editor.Unfocused += OnEditorUnFocused;
        base.OnAttachedTo(editor);
   }

    protected override void OnDetachingFrom(CustomEditor editor)
    {
        editor.BindingContextChanged -= OnBindingContextChanged;
        editor.Focused -= OnEditorFocused;
        editor.Unfocused -= OnEditorUnFocused;
        base.OnDetachingFrom(editor);
    }

    void OnBindingContextChanged(object sender, EventArgs args)
    {
        CustomEditor editor = sender as CustomEditor;
        if (string.IsNullOrEmpty(editor.Text))
        {
            editor.Text = editor.Placeholder;
            editor.TextColor = Color.FromHex(editor.PlaceholderColor);
        }
    }

    void OnEditorFocused(object sender, FocusEventArgs args)
    {
        CustomEditor editor = sender as CustomEditor;
        string placeholder = editor.Placeholder;
        string text = editor.Text;

        if (placeholder == text)
        {
            editor.Text = string.Empty;
        }
        editor.TextColor = Color.Default;
    }

    void OnEditorUnFocused(object sender, FocusEventArgs args)
    {
        CustomEditor editor = sender as CustomEditor;
        string placeholder = editor.Placeholder;
        string text = editor.Text;

        if (string.IsNullOrEmpty(text))
        {
            editor.Text = placeholder;
            editor.TextColor = Color.FromHex(editor.PlaceholderColor);
        }
        else
        {
            editor.TextColor = Color.Default;
        }
    }
}

This behavior, in nutshell, has only three events. First, when its Binding Context changed, it’s when the view model is set, we will also set our placeholder to the editor if it doesn’t have any text. The next two are on Focused and Unfocused, when user start and finish editing. The condition is the same, if it has any text, let it be. If it doesn’t, set the placeholder.

Let’s try it out!

To see how it works, all we have to do is attaching the behavior to the Custom Editor. In this example, I will attach it from the xaml. Here’s the sample code how to do it.

<ContentPage.Content>
    <StackLayout Orientation="Vertical">
        <control:CustomEditor Placeholder="{Binding Placeholder}" Text="{Binding Text}" HeightRequest="100">
            <Editor.Behaviors>
                <local:EditorBehavior />
            </Editor.Behaviors>
        </control:CustomEditor>
        <Label Text="Welcome to Editor Page" HorizontalOptions="Center"/>
    </StackLayout>
</ContentPage.Content>

And here’s the sample code of the view model that bind to the xaml file.

 

public class EditorViewModel : BaseViewModel
{
    protected string placeholder;
    public string Placeholder
    {
        get => placeholder;
        set => SetProperty(ref placeholder, value);
    }

    protected string text;
    public string Text
    {
        get => text;
        set => SetProperty(ref text, value);
    }

    public EditorViewModel()
    {
        Placeholder = "Write a Message";
    }
}

If you run the code, at first you’ll have an editor with a gray text on it. When you start typing, the gray text will be gone and replaced by you text with a normal text color.

6 thoughts on “Adding Placeholder to Editor

    1. Hi Carl, I’m not really sure what what’s wrong with your code, but wherever you put your custom editor, you need to add the folder in xaml so it can be called from xaml. Thanks.

      Like

      1. If my namespace is CarPooling and the custom editor is located in a map called Controls. Would this be the right approach?

        xmlns:controls=”clr-namespace:ElmiaGuide.Xamarin.Controls;assembly=ElmiaGuide.Xamarin.Controls”

        Like

      2. Define folder location inside content page bracket like this, I declare the location as “controls”, xmlns:controls=”clr-namespace:CarPooling.Controls” and then use that Custom Editor by referencing to controls like this controls:CustomEditor

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s