2.6 KiB
2.6 KiB
title | description | type | page_title | slug | position | tags | ticketid | res_type |
---|---|---|---|---|---|---|---|---|
Change Entry Cater Color when on focus | how to change the entry caret / cursor color on android | how-to | Change Entry Cursor color when on focus | entry-change-cursor-color | entry, caret, cursor, color, focus, xamarin, android, xamarin.forms | 1541339 | kb |
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.
- Entry definition:
<StackLayout>
<input:RadEntry WatermarkText="Telerik Entry"/>
</StackLayout>
- 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>
- 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>
- 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);
}
}
}
}