From e748b71158982dab5bc8db83a2077365de76956f Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 8 Feb 2017 03:27:43 -0500 Subject: [PATCH 1/5] Change cursor color when using selectionColor --- .../textinput/ReactTextInputManager.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) 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..d1870f674580 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,9 +11,11 @@ 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.text.Editable; @@ -310,6 +312,40 @@ 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 programatically. + // Based on http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android. + try { + // Get the cursor resource id. + Field field = TextView.class.getDeclaredField("mCursorDrawableRes"); + field.setAccessible(true); + int drawableResId = field.getInt(view); + + // Get the editor. + field = TextView.class.getDeclaredField("mEditor"); + field.setAccessible(true); + Object editor = field.get(view); + + // Get the drawable and set a color filter. + Drawable drawable = view.getContext().getDrawable(drawableResId); + if (color != null) { + drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); + } + Drawable[] drawables = {drawable, drawable}; + + // Set the drawables. + field = editor.getClass().getDeclaredField("mCursorDrawable"); + field.setAccessible(true); + field.set(editor, drawables); + } catch (Exception ex) { + // Ignore errors to avoid crashing if these private fields don't exist on modified + // or future android versions. + } } @ReactProp(name = "selectTextOnFocus", defaultBoolean = false) From e702c263cbbd57feba19d86971813f277f1b5e7a Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 8 Feb 2017 03:46:27 -0500 Subject: [PATCH 2/5] Update docs --- Libraries/Components/TextInput/TextInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, /** From b217f24e9420f764ba19fcb3eb0f91d7c7e972e1 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Tue, 14 Feb 2017 15:55:05 -0500 Subject: [PATCH 3/5] Cleanup SO code --- .../textinput/ReactTextInputManager.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) 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 d1870f674580..a5adb283a328 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 @@ -321,27 +321,24 @@ private void setCursorColor(ReactEditText view, @Nullable Integer color) { // the cursor color programatically. // Based on http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android. try { - // Get the cursor resource id. - Field field = TextView.class.getDeclaredField("mCursorDrawableRes"); - field.setAccessible(true); - int drawableResId = field.getInt(view); + // Get the original cusor drawable resource. + Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes"); + cursorDrawableResField.setAccessible(true); + int drawableResId = cursorDrawableResField.getInt(view); - // Get the editor. - field = TextView.class.getDeclaredField("mEditor"); - field.setAccessible(true); - Object editor = field.get(view); - - // Get the drawable and set a color filter. Drawable drawable = view.getContext().getDrawable(drawableResId); if (color != null) { drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); } Drawable[] drawables = {drawable, drawable}; - // Set the drawables. - field = editor.getClass().getDeclaredField("mCursorDrawable"); - field.setAccessible(true); - field.set(editor, drawables); + // 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 (Exception ex) { // Ignore errors to avoid crashing if these private fields don't exist on modified // or future android versions. From ba55d8cabf04c79c0077a448b7fb1bb6aef415d8 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 15 Feb 2017 16:25:46 -0500 Subject: [PATCH 4/5] Fix typos, use ContextCompat, catch specific exceptions --- .../react/views/textinput/ReactTextInputManager.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 a5adb283a328..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 @@ -18,6 +18,7 @@ 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; @@ -318,15 +319,15 @@ public void setSelectionColor(ReactEditText view, @Nullable Integer 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 programatically. + // the cursor color programmatically. // Based on http://stackoverflow.com/questions/25996032/how-to-change-programatically-edittext-cursor-color-in-android. try { - // Get the original cusor drawable resource. + // Get the original cursor drawable resource. Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes"); cursorDrawableResField.setAccessible(true); int drawableResId = cursorDrawableResField.getInt(view); - Drawable drawable = view.getContext().getDrawable(drawableResId); + Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId); if (color != null) { drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); } @@ -339,10 +340,10 @@ private void setCursorColor(ReactEditText view, @Nullable Integer color) { Field cursorDrawableField = editor.getClass().getDeclaredField("mCursorDrawable"); cursorDrawableField.setAccessible(true); cursorDrawableField.set(editor, drawables); - } catch (Exception ex) { + } 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) From 2f836ec83b457544ae7f89167b63c55e52a0df4a Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 15 Feb 2017 18:50:22 -0500 Subject: [PATCH 5/5] Fix buck build --- .../src/main/java/com/facebook/react/views/textinput/BUCK | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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', ], ) -