question

JohnHardman-6852 avatar image
0 Votes"
JohnHardman-6852 asked RobCaplan edited

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

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://docs.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 )

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If anyone is still looking for it, then setting "colorAccent" in manifest will do the trick

<item name="colorAccent">@color/colorAccent</item>


https://developer.android.com/guide/topics/ui/look-and-feel/themes

0 Votes 0 ·

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered WeiWen-3421 commented

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.



image.png (6.6 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT - How can this be done dynamically (i.e. C# setting the cursor color, rather than the color being fixed in a xml file)?

0 Votes 0 ·

I tried with following code,it could get the xml file's GradientDrawable, but it cannot change the cursor's color dynamically, it is not worked in xamarin,

:Here is native android's solution. https://stackoverflow.com/questions/19864337/how-can-i-change-colors-in-my-statelistdrawable

StateListDrawable gradientDrawable = (StateListDrawable)ContextCompat.GetDrawable(Android.App.Application.Context, Resource.Drawable.xml_file_name);
DrawableContainer.DrawableContainerState drawableContainerState = gradientDrawable.GetConstantState() as DrawableContainer.DrawableContainerState;
 var chilrens = drawableContainerState.GetChildren();
GradientDrawable selectedItem = chilrens[0] as GradientDrawable;
selectedItem.SetStroke(2, Android.Graphics.Color.Red);


So, here is a workaround, If you have serveral color cursors, you can create different xml files in the drawable folder. Different xml files have different solid color to achieve it.

0 Votes 0 ·

@LeonLu-MSFT I tried your method. On my Mac, it works great. But when I ran on my Windows, I got this error:
Java.Lang.NoSuchMethodError: 'no non-static method "Landroid/widget/EditText;.setTextCursorDrawable(I)V"'

Does this mean I need to install Android Studio on my Windows or download some Android SDK?

0 Votes 0 ·