Skip to content

Commit 0067805

Browse files
committed
TintHelper can tint cursor of EditTexts.
1 parent 47b115f commit 0067805

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

core/src/main/java/com/afollestad/materialdialogs/internal/MDTintHelper.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
import android.widget.ProgressBar;
1616
import android.widget.RadioButton;
1717
import android.widget.SeekBar;
18+
import android.widget.TextView;
1819

1920
import com.afollestad.materialdialogs.R;
2021
import com.afollestad.materialdialogs.util.DialogUtils;
2122

23+
import java.lang.reflect.Field;
24+
2225
/**
2326
* Tints widgets
2427
*/
@@ -112,6 +115,7 @@ public static void setTint(@NonNull EditText editText, @ColorInt int color) {
112115
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
113116
editText.setBackgroundTintList(editTextColorStateList);
114117
}
118+
setCursorTint(editText, color);
115119
}
116120

117121
public static void setTint(@NonNull CheckBox box, @ColorInt int color) {
@@ -130,4 +134,26 @@ public static void setTint(@NonNull CheckBox box, @ColorInt int color) {
130134
box.setButtonDrawable(drawable);
131135
}
132136
}
133-
}
137+
138+
private static void setCursorTint(@NonNull EditText editText, @ColorInt int color) {
139+
try {
140+
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
141+
fCursorDrawableRes.setAccessible(true);
142+
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
143+
Field fEditor = TextView.class.getDeclaredField("mEditor");
144+
fEditor.setAccessible(true);
145+
Object editor = fEditor.get(editText);
146+
Class<?> clazz = editor.getClass();
147+
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
148+
fCursorDrawable.setAccessible(true);
149+
Drawable[] drawables = new Drawable[2];
150+
drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
151+
drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
152+
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
153+
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
154+
fCursorDrawable.set(editor, drawables);
155+
} catch (Exception e) {
156+
e.printStackTrace();
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)