@@ -158,6 +158,15 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
158
158
private static final String KEYBOARD_TYPE_URI = "url" ;
159
159
private static final InputFilter [] EMPTY_FILTERS = new InputFilter [0 ];
160
160
private static final int UNSET = -1 ;
161
+ private static final String [] DRAWABLE_FIELDS = {
162
+ "mCursorDrawable" , "mSelectHandleLeft" , "mSelectHandleRight" , "mSelectHandleCenter"
163
+ };
164
+ private static final String [] DRAWABLE_RESOURCES = {
165
+ "mCursorDrawableRes" ,
166
+ "mTextSelectHandleLeftRes" ,
167
+ "mTextSelectHandleRightRes" ,
168
+ "mTextSelectHandleRes"
169
+ };
161
170
162
171
protected @ Nullable ReactTextViewManagerCallback mReactTextViewManagerCallback ;
163
172
@@ -507,34 +516,40 @@ public void setCursorColor(ReactEditText view, @Nullable Integer color) {
507
516
}
508
517
509
518
// The evil code that follows uses reflection to achieve this on Android 8.1 and below.
510
- // Based on
511
- // http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android.
512
- try {
513
- // Get the original cursor drawable resource.
514
- Field cursorDrawableResField = TextView .class .getDeclaredField ("mCursorDrawableRes" );
515
- cursorDrawableResField .setAccessible (true );
516
- int drawableResId = cursorDrawableResField .getInt (view );
517
-
518
- // The view has no cursor drawable.
519
- if (drawableResId == 0 ) {
520
- return ;
521
- }
519
+ // Based on https://tinyurl.com/3vff8lyu https://tinyurl.com/vehggzs9
520
+ for (int i = 0 ; i < DRAWABLE_RESOURCES .length ; i ++) {
521
+ try {
522
+ Field drawableResourceField = TextView .class .getDeclaredField (DRAWABLE_RESOURCES [i ]);
523
+ drawableResourceField .setAccessible (true );
524
+ int resourceId = drawableResourceField .getInt (view );
522
525
523
- Drawable drawable = ContextCompat .getDrawable (view .getContext (), drawableResId );
524
- drawable .setColorFilter (color , PorterDuff .Mode .SRC_IN );
525
- Drawable [] drawables = {drawable , drawable };
526
-
527
- // Update the current cursor drawable with the new one.
528
- Field editorField = TextView .class .getDeclaredField ("mEditor" );
529
- editorField .setAccessible (true );
530
- Object editor = editorField .get (view );
531
- Field cursorDrawableField = editor .getClass ().getDeclaredField ("mCursorDrawable" );
532
- cursorDrawableField .setAccessible (true );
533
- cursorDrawableField .set (editor , drawables );
534
- } catch (NoSuchFieldException ex ) {
535
- // Ignore errors to avoid crashing if these private fields don't exist on modified
536
- // or future android versions.
537
- } catch (IllegalAccessException ex ) {
526
+ // The view has no cursor drawable.
527
+ if (resourceId == 0 ) {
528
+ return ;
529
+ }
530
+
531
+ Drawable drawable = ContextCompat .getDrawable (view .getContext (), resourceId );
532
+
533
+ Drawable drawableCopy = drawable .mutate ();
534
+ drawableCopy .setColorFilter (color , PorterDuff .Mode .SRC_IN );
535
+
536
+ Field editorField = TextView .class .getDeclaredField ("mEditor" );
537
+ editorField .setAccessible (true );
538
+ Object editor = editorField .get (view );
539
+
540
+ Field cursorDrawableField = editor .getClass ().getDeclaredField (DRAWABLE_FIELDS [i ]);
541
+ cursorDrawableField .setAccessible (true );
542
+ if (DRAWABLE_RESOURCES [i ] == "mCursorDrawableRes" ) {
543
+ Drawable [] drawables = {drawableCopy , drawableCopy };
544
+ cursorDrawableField .set (editor , drawables );
545
+ } else {
546
+ cursorDrawableField .set (editor , drawableCopy );
547
+ }
548
+ } catch (NoSuchFieldException ex ) {
549
+ // Ignore errors to avoid crashing if these private fields don't exist on modified
550
+ // or future android versions.
551
+ } catch (IllegalAccessException ex ) {
552
+ }
538
553
}
539
554
}
540
555
0 commit comments