mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
cd1344620c
commit
e7a14b803f
+20
-2
@@ -11,6 +11,8 @@ import static com.facebook.react.uimanager.UIManagerHelper.getReactContext;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.BlendMode;
|
||||
import android.graphics.BlendModeColorFilter;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
@@ -433,8 +435,24 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
|
||||
|
||||
@ReactProp(name = "cursorColor", customType = "Color")
|
||||
public void setCursorColor(ReactEditText view, @Nullable Integer color) {
|
||||
// Evil method that uses reflection because there is no public API to changes
|
||||
// the cursor color programmatically.
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
Drawable cursorDrawable = view.getTextCursorDrawable();
|
||||
if (cursorDrawable != null) {
|
||||
cursorDrawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_IN));
|
||||
view.setTextCursorDrawable(cursorDrawable);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
|
||||
// Pre-Android 10, there was no supported API to change the cursor color programmatically.
|
||||
// In Android 9.0, they changed the underlying implementation,
|
||||
// but also "dark greylisted" the new field, rendering it unusable.
|
||||
return;
|
||||
}
|
||||
|
||||
// The evil code that follows uses reflection to achieve this on Android 8.1 and below.
|
||||
// Based on
|
||||
// http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android.
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user