|
11 | 11 |
|
12 | 12 | import android.content.Context;
|
13 | 13 | import android.content.res.ColorStateList;
|
| 14 | +import android.graphics.BlendMode; |
| 15 | +import android.graphics.BlendModeColorFilter; |
14 | 16 | import android.graphics.PorterDuff;
|
15 | 17 | import android.graphics.drawable.Drawable;
|
16 | 18 | import android.os.Build;
|
@@ -433,8 +435,24 @@ public void setSelectionColor(ReactEditText view, @Nullable Integer color) {
|
433 | 435 |
|
434 | 436 | @ReactProp(name = "cursorColor", customType = "Color")
|
435 | 437 | public void setCursorColor(ReactEditText view, @Nullable Integer color) {
|
436 |
| - // Evil method that uses reflection because there is no public API to changes |
437 |
| - // the cursor color programmatically. |
| 438 | + |
| 439 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 440 | + Drawable cursorDrawable = view.getTextCursorDrawable(); |
| 441 | + if (cursorDrawable != null) { |
| 442 | + cursorDrawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_IN)); |
| 443 | + view.setTextCursorDrawable(cursorDrawable); |
| 444 | + } |
| 445 | + return; |
| 446 | + } |
| 447 | + |
| 448 | + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) { |
| 449 | + // Pre-Android 10, there was no supported API to change the cursor color programmatically. |
| 450 | + // In Android 9.0, they changed the underlying implementation, |
| 451 | + // but also "dark greylisted" the new field, rendering it unusable. |
| 452 | + return; |
| 453 | + } |
| 454 | + |
| 455 | + // The evil code that follows uses reflection to achieve this on Android 8.1 and below. |
438 | 456 | // Based on
|
439 | 457 | // http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android.
|
440 | 458 | try {
|
|
0 commit comments