Skip to content

Commit e7a14b8

Browse files
Nicholas Tinsleyfacebook-github-bot
Nicholas Tinsley
authored andcommitted
Fix CursorDrawable Color Tint for Android 10+
Summary: Accessing this field via reflection is explicitly blacklisted by Google in Android 10 and higher. They have provided a new API to change the color, which I have implemented here. [The old setColorFilter is deprecated](https://developer.android.com/reference/android/graphics/drawable/Drawable#setColorFilter(int,%20android.graphics.PorterDuff.Mode)) so I also updated that method call as well. Changelog: [General] [Fixed] Use new setTextCursorDrawable API for Android 10 Reviewed By: JoshuaGross Differential Revision: D20656068 fbshipit-source-id: 58a92b57c0a892c7c87fc5d735e4ceaa4e987ec7
1 parent cd13446 commit e7a14b8

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import android.content.Context;
1313
import android.content.res.ColorStateList;
14+
import android.graphics.BlendMode;
15+
import android.graphics.BlendModeColorFilter;
1416
import android.graphics.PorterDuff;
1517
import android.graphics.drawable.Drawable;
1618
import android.os.Build;
@@ -433,8 +435,24 @@ public void setSelectionColor(ReactEditText view, @Nullable Integer color) {
433435

434436
@ReactProp(name = "cursorColor", customType = "Color")
435437
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.
438456
// Based on
439457
// http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android.
440458
try {

0 commit comments

Comments
 (0)