Ranjith Kumar Nagiri
Ranjith Kumar Nagiri

Reputation: 863

Change EditText Cursor Color in Xamarin Forms

I have an Entry in my ContentPage and am doing rendering in Xamarin Android. Here my problem is EditText background color is white and cursor color is also white. Here I want to change the cursor color to black. Is there any way to change the cursor color? Here is my code.

Entry to ExtendedEntry :

public class ExtendedEntry : Entry { }

Use ExtendedEntry in Content Page :

var txtPhoneNumber = new ExtendedEntry { Placeholder = "Phone Number", Keyboard = Keyboard.Numeric, TextColor = Color.Black };

Render the ExtendedEntry in Xamarin Android :

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRender))]
namespace Project.Droid
{
    public class ExtendedEntryRender : EntryRenderer
    {
        // Override the OnElementChanged method so we can tweak this renderer post-initial setup
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null)
            {   // perform initial setup
                // lets get a reference to the native control
                var nativeEditText = (global::Android.Widget.EditText)Control;
                // do whatever you want to the textField here!
            nativeEditText.SetBackgroundResource(Resource.Drawable.text_box);
            }
        }
    }
}

Can any one help me to solve this problem? Thanks in advance.

Upvotes: 2

Views: 2761

Answers (2)

Kala J
Kala J

Reputation: 2070

You can change the cursor color using:

IntPtr IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
IntPtr mCursorDrawableResProperty = JNIEnv.GetFieldID (IntPtrtextViewClass, "mCursorDrawableRes", "I");
JNIEnv.SetField (Control.Handle, mCursorDrawableResProperty, 0); // replace 0 with a Resource.Drawable.my_cursor 

0 will keep the same color as the TextColor on your Entry.

Upvotes: 3

user3715225
user3715225

Reputation: 11

Edit: To change the cursur color the only option is to change the theme by adding for example:

Theme = "android:style/Theme.Holo.light" in MainActivity

Upvotes: -2

Related Questions