diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index ffc25757a9f9..7bccf38b9a5d 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -409,7 +409,7 @@ const TextInput = React.createClass({ */ secureTextEntry: PropTypes.bool, /** - * The highlight (and cursor on iOS) color of the text input. + * The highlight and cursor color of the text input. */ selectionColor: ColorPropType, /** diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK index b14cc057f73b..27e87e7fd0b6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK @@ -5,6 +5,7 @@ android_library( srcs = glob(['*.java']), deps = [ YOGA_TARGET, + react_native_dep('third-party/android/support/v4:lib-support-v4'), react_native_dep('third-party/java/infer-annotations:infer-annotations'), react_native_dep('third-party/java/jsr-305:jsr-305'), react_native_target('java/com/facebook/react/bridge:bridge'), @@ -22,4 +23,3 @@ android_library( 'PUBLIC', ], ) - diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index b0ed4cf8352b..e91f681af947 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -11,11 +11,14 @@ import javax.annotation.Nullable; +import java.lang.reflect.Field; import java.util.LinkedList; import java.util.Map; +import android.graphics.drawable.Drawable; import android.graphics.PorterDuff; import android.graphics.Typeface; +import android.support.v4.content.ContextCompat; import android.text.Editable; import android.text.InputFilter; import android.text.InputType; @@ -310,6 +313,37 @@ public void setSelectionColor(ReactEditText view, @Nullable Integer color) { } else { view.setHighlightColor(color); } + + setCursorColor(view, color); + } + + private 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); + + 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) {} } @ReactProp(name = "selectTextOnFocus", defaultBoolean = false)