Skip to content

Fix cursorColor on Android Q+ #26433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .buckconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

[android]
target = android-28
compile_sdk_version = android-29

[download]
max_number_of_retries = 3
Expand Down
15 changes: 15 additions & 0 deletions RNTester/js/examples/TextInput/TextInputExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,19 @@ exports.examples = ([
return <ToggleDefaultPaddingExample />;
},
},
{
title: 'Colored cursor for text input',
render: function(): React.Node {
return (
<View>
<TextInput
style={styles.default}
cursorColor="red"
defaultValue="Highlight me"
/>
</View>
);
},
},
]: Array<RNTesterExampleModuleItem>);

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import static com.facebook.react.uimanager.UIManagerHelper.getReactContext;

import android.graphics.BlendMode;
import android.graphics.BlendModeColorFilter;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
Expand Down Expand Up @@ -435,38 +437,49 @@ public void setSelectionColor(ReactEditText view, @Nullable Integer color) {

@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.
// Based on
// http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android.
try {
// Get the original cursor drawable resource.
Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
cursorDrawableResField.setAccessible(true);
int drawableResId = cursorDrawableResField.getInt(view);

// The view has no cursor drawable.
if (drawableResId == 0) {
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Drawable drawable = view.getTextCursorDrawable();
if (drawable != null) {
if (color != null) {
drawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_IN));
} else {
drawable.setColorFilter(null);
}
view.setTextCursorDrawable(drawable);
}
} else {
// Evil method that uses reflection because there is no public API to changes
// the cursor color programmatically.
// Based on
// http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android.
try {
// Get the original cursor drawable resource.
Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
cursorDrawableResField.setAccessible(true);
int drawableResId = cursorDrawableResField.getInt(view);

// The view has no cursor drawable.
if (drawableResId == 0) {
return;
}

Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId);
if (color != null) {
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
Drawable[] drawables = {drawable, drawable};

// Update the current cursor drawable with the new one.
Field editorField = TextView.class.getDeclaredField("mEditor");
editorField.setAccessible(true);
Object editor = editorField.get(view);
Field cursorDrawableField = editor.getClass().getDeclaredField("mCursorDrawable");
cursorDrawableField.setAccessible(true);
cursorDrawableField.set(editor, drawables);
} catch (NoSuchFieldException ex) {
// Ignore errors to avoid crashing if these private fields don't exist on modified
// or future android versions.
} catch (IllegalAccessException ex) {
Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId);
if (color != null) {
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
Drawable[] drawables = {drawable, drawable};

// Update the current cursor drawable with the new one.
Field editorField = TextView.class.getDeclaredField("mEditor");
editorField.setAccessible(true);
Object editor = editorField.get(view);
Field cursorDrawableField = editor.getClass().getDeclaredField("mCursorDrawable");
cursorDrawableField.setAccessible(true);
cursorDrawableField.set(editor, drawables);
} catch (NoSuchFieldException ex) {
// Ignore errors to avoid crashing if these private fields don't exist on modified
// or future android versions.
} catch (IllegalAccessException ex) {}
}
}

Expand Down