Exemple #1
0
            public static void SetCursorColor(EditText editText, Color color)
            {
                try
                {
                    if (!_prepared)
                    {
                        PrepareFields(editText.Context);
                    }

                    if (_cursorDrawableField == null || _cursorDrawableResField == null || _editorField == null || PorterDuff.Mode.SrcIn == null)
                    {
                        // PrepareFields() failed, give up now
                        editText.Log().WarnIfEnabled(() => "Failed to change the cursor color. Some devices don't support this.");
                        return;
                    }

                    var mCursorDrawableRes = _cursorDrawableResField.GetInt(editText);
                    var editor             = _editorField.Get(editText);

#if __ANDROID_28__
#pragma warning disable 618                             // SetColorFilter is deprecated
                    if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
                    {
                        var drawables = new Drawable[2];
                        drawables[0] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawables[1] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawables[0].SetColorFilter(color, PorterDuff.Mode.SrcIn);
                        drawables[1].SetColorFilter(color, PorterDuff.Mode.SrcIn);
                        _cursorDrawableField.Set(editor, drawables);
                    }
                    else
                    {
                        var drawable = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);
                        _cursorDrawableField.Set(editor, drawable);
                    }
#pragma warning restore 618                             // SetColorFilter is deprecated
#else
                    if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
                    {
                        var drawables = new Drawable[2];
                        drawables[0] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawables[1] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawables[0].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
                        drawables[1].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
                        _cursorDrawableField.Set(editor, drawables);
                    }
                    else
                    {
                        var drawable = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawable.SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
                        _cursorDrawableField.Set(editor, drawable);
                    }
#endif
                }
                catch (Exception)
                {
                    editText.Log().WarnIfEnabled(() => "Failed to change the cursor color. Some devices don't support this.");
                }
            }
Exemple #2
0
            public static void SetCursorColor(EditText editText, Color color)
            {
                try
                {
                    if (!_prepared)
                    {
                        PrepareFields(editText.Context);
                    }

                    var mCursorDrawableRes = _cursorDrawableResField.GetInt(editText);
                    var editor             = _editorField.Get(editText);
                    if ((int)Build.VERSION.SdkInt < 28)                     // 28 means BuildVersionCodes.P
                    {
                        var drawables = new Drawable[2];
                        drawables[0] = Android.Support.V4.Content.ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawables[1] = Android.Support.V4.Content.ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawables[0].SetColorFilter(color, PorterDuff.Mode.SrcIn);
                        drawables[1].SetColorFilter(color, PorterDuff.Mode.SrcIn);
                        _cursorDrawableField.Set(editor, drawables);
                    }
                    else
                    {
                        var drawable = Android.Support.V4.Content.ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                        drawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);
                        _cursorDrawableField.Set(editor, drawable);
                    }
                }
                catch (Exception)
                {
                    editText.Log().WarnIfEnabled(() => "Failed to change the cursor color. Some devices don't support this.");
                }
            }
            public static void SetCursorColor(EditText editText, Color color)
            {
                try
                {
                    if (!_prepared)
                    {
                        PrepareFields(editText.Context);
                    }

                    var mCursorDrawableRes = _cursorDrawableResField.GetInt(editText);
                    var drawables          = new Drawable[2];
                    drawables[0] = Android.Support.V4.Content.ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                    drawables[1] = Android.Support.V4.Content.ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
                    drawables[0].SetColorFilter(color, PorterDuff.Mode.SrcIn);
                    drawables[1].SetColorFilter(color, PorterDuff.Mode.SrcIn);
                    var editor = _editorField.Get(editText);
                    _cursorDrawableField.Set(editor, drawables);
                }
                catch (Exception ex)
                {
                    editText.Log().Error("Failed to change the cursor color.", ex);
                }
            }