Fix edit text handle colors
This commit is contained in:
parent
1145405832
commit
b7ed2488f2
9 changed files with 72 additions and 115 deletions
|
@ -1,33 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.appthemehelper.common.views
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import code.name.monkey.appthemehelper.ATH
|
||||
import code.name.monkey.appthemehelper.ThemeStore
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
|
||||
class ATEEditText @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : TextInputEditText(context, attrs, defStyleAttr) {
|
||||
|
||||
init {
|
||||
ATH.setTint(this, ThemeStore.accentColor(context))
|
||||
setTextColor(ThemeStore.textColorPrimary(context))
|
||||
}
|
||||
}
|
|
@ -47,31 +47,6 @@ object MaterialUtil {
|
|||
textInputLayout.defaultHintTextColor = colorState
|
||||
textInputLayout.isHintAnimationEnabled = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun setCursorPointerColor(view: EditText, @ColorInt color: Int) {
|
||||
try {
|
||||
//get the pointer resource id
|
||||
var field = TextView::class.java.getDeclaredField("mTextSelectHandleRes")
|
||||
field.isAccessible = true
|
||||
val drawableResId = field.getInt(view)
|
||||
|
||||
//get the editor
|
||||
field = TextView::class.java.getDeclaredField("mEditor")
|
||||
field.isAccessible = true
|
||||
val editor = field.get(view)
|
||||
|
||||
//tint drawable
|
||||
val drawable = ContextCompat.getDrawable(view.context, drawableResId)!!
|
||||
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN)
|
||||
|
||||
//set the drawable
|
||||
field = editor.javaClass.getDeclaredField("mSelectHandleCenter")
|
||||
field.isAccessible = true
|
||||
field.set(editor, drawable)
|
||||
|
||||
} catch (ex: Exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -407,4 +407,45 @@ public final class TintHelper {
|
|||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("JavaReflectionMemberAccess")
|
||||
public static void colorHandles(@NonNull TextView view, int color) {
|
||||
try {
|
||||
Field editorField = TextView.class.getDeclaredField("mEditor");
|
||||
if (!editorField.isAccessible()) {
|
||||
editorField.setAccessible(true);
|
||||
}
|
||||
|
||||
Object editor = editorField.get(view);
|
||||
Class<?> editorClass = editor.getClass();
|
||||
|
||||
String[] handleNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
|
||||
String[] resNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};
|
||||
|
||||
for (int i = 0; i < handleNames.length; i++) {
|
||||
Field handleField = editorClass.getDeclaredField(handleNames[i]);
|
||||
if (!handleField.isAccessible()) {
|
||||
handleField.setAccessible(true);
|
||||
}
|
||||
|
||||
Drawable handleDrawable = (Drawable) handleField.get(editor);
|
||||
|
||||
if (handleDrawable == null) {
|
||||
Field resField = TextView.class.getDeclaredField(resNames[i]);
|
||||
if (!resField.isAccessible()) {
|
||||
resField.setAccessible(true);
|
||||
}
|
||||
int resId = resField.getInt(view);
|
||||
handleDrawable = view.getResources().getDrawable(resId);
|
||||
}
|
||||
|
||||
if (handleDrawable != null) {
|
||||
Drawable drawable = handleDrawable.mutate();
|
||||
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
handleField.set(editor, drawable);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue