New to Telerik UI for Xamarin? Download free 30-day trial

Change Entry Cater Color when on focus

Environment

Product Version 2021.3.915.1
Product Entry for Xamarin Cross-Platform

Description

This article shows how to change the Entry cursor / caret color on Android when the control receives focus.

Solution

You will need a Custom Renderer for Android.

  1. Entry definition:
<StackLayout>
    <input:RadEntry WatermarkText="Telerik Entry"/>
</StackLayout>
  1. Inside the Android/Resources/drawable folder create a my_cursor.xml file for the caret / cursor
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

  <solid android:color="@color/colorCursors"></solid>
  <size android:width="2dp" />

</shape>
  1. Add a color for the cursor - colorCursor inside colors.xml file inside the Android/Resources/values folder:
<resources>
    <color name="launcher_background">#FFFFFF</color>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="colorCursors">#303F9F</color>
</resources>
  1. Create a Custom Renderer on Android. Class CustomEntryRenderer which inherits from EntryRenderer.

In oder to change the caret / cursor color override the OnElementChanged method and inside it call the SetTextCursorDrawable method to change the cursor color and consume the resource:

using Android.Content;
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Text;
using Android.Widget;
using ListPicker.Droid;
using Telerik.XamarinForms.Input;
using Telerik.XamarinForms.InputRenderer.Android;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(RadEntry), typeof(CustomEntryRenderer))]

namespace ListPicker.Droid
{
    public class CustomEntryRenderer : EntryRenderer
    {
        public CustomEntryRenderer(Context context) : base (context)
        {

        }

        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<RadEntry> e)
        {
            base.OnElementChanged(e);

            if (this.Control != null)
            {
                this.Control.SetTextCursorDrawable(Resource.Drawable.my_cursor);
            }
        }
    }
}
In this article