Change Entry Cursor

jzeferinojzeferino USUniversity ✭✭

Hello.
I have a page and the background must be black. In that page i have an Entry.
When the Entry have focus i can't see the cursor blinking because it is black.

I have tried to make a custom render for the Entry and in android i tried this solution (i used it before on android):

    try {
        // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
        Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
        f.setAccessible(true);
        f.set(yourEditText, R.drawable.cursor);
    } catch (Exception ignored) {
    }

the problem is that the field "mCursorDrawableRes" isn't visible in C# (java.lang.NoSuchFieldException: mCursorDrawableRes):

    protected override void OnElementChanged (ElementChangedEventArgs<Entry> e) 
            {
                base.OnElementChanged (e);

                var entry = (TextView)Control;

                var field = entry.Class.GetDeclaredField("mCursorDrawableRes");

    ...

Does anyone have a solution for this?

Best Answer

Answers

  • RendyDelRosarioRendyDelRosario DOUniversity ✭✭✭

    Try adding a drawable for the edittext , for example here for setting it white:

    edittext_white.xml:

    //edittext_white.xml
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_dark" />
        <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled_holo_dark" />
        <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_activated_holo_dark" />
        <item android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_focused_holo_dark" />
        <item android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_dark" />
        <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_focused_holo_dark" />
        <item android:drawable="@drawable/textfield_disabled_holo_dark" />
    </selector>
    

    And add this style to your styles.xml:

      <style name="EditTextMyTheme" parent="android:Widget.EditText">
          <item name="android:background">@drawable/edittext_white</item>
          <item name="android:textColor">#ffffff</item>
      </style>
    

    Note: In this case i am using Holo theme

  • jzeferinojzeferino USUniversity ✭✭
    edited June 2015

    In my case the EditText is already an instance.
    So i have to change the style of the EditText programmatically and i think it is not possible.

  • jzeferinojzeferino USUniversity ✭✭

    @AndreiNitescu thank you for the clear solution.
    With JNIEnv i would probably do some more interesting stuff.

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    do it only if really needed.
    otherwise use other ways. I don't know why you can't use theming here for example like @RendyDelRosario suggested

  • jzeferinojzeferino USUniversity ✭✭

    i can't apply the theme because the EditText is already instanciated.

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    I have the same problem. The solution of @AndrewMobile works, but I'm set now with that color. As the drawable has to be defined in the resources of the assembly of my Customrenderer, I can't change it if I want to use the Renderer in another project easily.

    If there would be another solution it would be great ideally I would like to be able to set the CursorColor from a property of my Control.

  • priyakarthicpriyakarthic INMember

    Hi how do i remove the default android entry bottom line in xamarin forms pcl. I have tried to make a custom render for the Entry,

    public class EntryCustomRenderer : EntryRenderer
    {
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);
    Control?.SetBackgroundColor(Android.Graphics.Color.White);
    }
    }

    but cursor is not visible in entry. can anyone help me to solve this

  • @priyakarthic
    use Control.SetBackgroundDrawable(null);

    if you want to just change the line, you can use

    Control.BackgroundTintList = ColorStateList.ValueOf(e.NewElement.TextColor.ToAndroid());

  • denominator01denominator01 USMember ✭✭
    edited November 2016

    @AndrewMobile said:

    I think you're on the right track. try this:

    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 
    

    Note that if you have a TextColor set for the Entry, the cursor will use that color if you keep '0' value for resource id

    @zeferino hope this answers your question

    This does not work on different platforms. It worked on OS 5 but not OS 6. Is there a way to make it work across different OS/devices?

  • jzeferinojzeferino USUniversity ✭✭

    This solution is for Android only. What do you mean by OS 5 or 6?

Sign In or Register to comment.