How to dynamically set cursor color for Entry and Editor on Android in XF 5.0 ?

John Hardman 261 Reputation points
2021-02-09T15:11:39.967+00:00

Having just upgraded from XF 4.8 to 5.0, I've had to migrate to using AndroidX. As a result, the piece of code that is found in multiple locations when Googling that allows the cursor color for Xamarin.Forms Entry and Editor Views to be set dynamically no longer works.

Instead, that code (see below) now throws
Java.Lang.NoSuchFieldError: no "I" field "mCursorDrawableRes" in class "Landroid/widget/TextView;" or its superclasses

IntPtr IntPtrTextViewClass = JNIEnv.FindClass(typeof(TextView));  
IntPtr mCursorDrawableResProperty = JNIEnv.GetFieldID(IntPtrTextViewClass, "mCursorDrawableRes", "I");  

For iOS, Xamarin now provide a SetCursorColor method (see https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.platformconfiguration.iosspecific.entry.setcursorcolor?view=xamarin-forms ).

How can we dynamically set the cursor color for Entry and Editor on Android when using XF 5.0 ?

(cross posted from https://forums.xamarin.com/discussion/187355/how-to-set-cursor-color-for-entry-and-editor-when-targeting-android )

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2021-02-10T12:06:16.193+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can create entry's custom-renderer for this issue.

       [assembly: ExportRenderer(typeof(Entry), typeof(MyEntry))]  
       namespace App38.Droid  
       {  
             
           class MyEntry : EntryRenderer  
           {  
               public MyEntry(Context context) : base(context)  
               {  
               }  
         
               protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
               {  
                   base.OnElementChanged(e);  
         
                   if (Control != null)  
                   {  
                       EditText nativeEditText = (global::Android.Widget.EditText)Control;  
                       nativeEditText.SetTextCursorDrawable(Resource.Drawable.xml_file_name);  
                         
         
         
                   }  
               }  
           }  
       }  
    

    Here is xml_file_name.xml in Drawable folder.

       <?xml version="1.0" encoding="utf-8"?>  
       <selector xmlns:android="http://schemas.android.com/apk/res/android">  
         <item>  
           <shape>  
             <!-- edit text cursor width 2 dp -->  
             <size android:width="2dp"/>  
             <!-- edit text cursor color red -->  
             <solid android:color="#00ff00"/>  
           </shape>  
         </item>  
       </selector>  
    

    Here is running screenshot.

    66443-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.