4
4
import android .content .res .Resources ;
5
5
import android .content .res .TypedArray ;
6
6
import android .graphics .Color ;
7
- import android .graphics .drawable .GradientDrawable ;
8
- import android .graphics .drawable .LayerDrawable ;
7
+ import android .graphics .PorterDuff ;
8
+ import android .graphics .PorterDuffColorFilter ;
9
+ import android .graphics .drawable .Drawable ;
10
+ import android .support .v4 .content .ContextCompat ;
9
11
import android .support .v4 .view .ViewCompat ;
10
12
import android .support .v7 .widget .AppCompatImageButton ;
11
13
import android .support .v7 .widget .AppCompatTextView ;
16
18
import android .view .View ;
17
19
import android .view .ViewGroup ;
18
20
import android .view .inputmethod .InputMethodManager ;
21
+ import android .widget .EditText ;
19
22
import android .widget .FrameLayout ;
20
23
import android .widget .RelativeLayout ;
24
+ import android .widget .TextView ;
25
+
26
+ import java .lang .reflect .Field ;
21
27
22
28
23
29
/**
24
30
* Text Field Boxes
25
31
* Created by CarbonylGroup on 2017/08/25
26
32
*/
33
+ @ SuppressWarnings ("unused" )
27
34
public class TextFieldBoxes extends FrameLayout {
28
35
29
36
/**
@@ -155,16 +162,16 @@ protected void initDefaultColor() {
155
162
TypedArray themeArray ;
156
163
157
164
/* Get Default Error Color From Theme */
158
- DEFAULT_ERROR_COLOR = getContext (). getResources (). getColor (R .color .A400red );
165
+ DEFAULT_ERROR_COLOR = ContextCompat . getColor (getContext (), R .color .A400red );
159
166
160
167
/* Get Default Background Color From Theme */
161
168
themeArray = theme .obtainStyledAttributes (new int []{android .R .attr .colorForeground });
162
- DEFAULT_BG_COLOR = Utils . adjustAlpha (themeArray .getColor (0 , 0 ), 0.06f );
169
+ DEFAULT_BG_COLOR = adjustAlpha (themeArray .getColor (0 , 0 ), 0.06f );
163
170
164
171
/* Get Default Primary Color From Theme */
165
172
themeArray = theme .obtainStyledAttributes (new int []{R .attr .colorPrimary });
166
- if (Utils . isLight (DEFAULT_BG_COLOR ))
167
- DEFAULT_PRIMARY_COLOR = Utils . lighter (themeArray .getColor (0 , 0 ), 0.2f );
173
+ if (isLight (DEFAULT_BG_COLOR ))
174
+ DEFAULT_PRIMARY_COLOR = lighter (themeArray .getColor (0 , 0 ), 0.2f );
168
175
else DEFAULT_PRIMARY_COLOR = themeArray .getColor (0 , 0 );
169
176
170
177
/* Get Default Text Color From Theme */
@@ -175,7 +182,7 @@ protected void initDefaultColor() {
175
182
themeArray = theme .obtainStyledAttributes (new int []{android .R .attr .disabledAlpha });
176
183
float disabledAlpha = themeArray .getFloat (0 , 0 );
177
184
themeArray = theme .obtainStyledAttributes (new int []{android .R .attr .textColorTertiary });
178
- DEFAULT_DISABLED_TEXT_COLOR = Utils . adjustAlpha (themeArray .getColor (0 , 0 ), disabledAlpha );
185
+ DEFAULT_DISABLED_TEXT_COLOR = adjustAlpha (themeArray .getColor (0 , 0 ), disabledAlpha );
179
186
180
187
themeArray .recycle ();
181
188
}
@@ -526,7 +533,7 @@ protected void makeCursorBlink() {
526
533
protected void setHighlightColor (int colorRes ) {
527
534
528
535
this .floatingLabel .setTextColor (colorRes );
529
- Utils . setCursorDrawableColor (this .editText , colorRes );
536
+ setCursorDrawableColor (this .editText , colorRes );
530
537
531
538
if (getIsResponsiveIconColor ()) {
532
539
this .iconImageButton .setColorFilter (colorRes );
@@ -555,7 +562,8 @@ protected void updateCounterText() {
555
562
else showClearButton (true );
556
563
557
564
/* Don't Count Space & Line Feed */
558
- int length = this .editText .getText ().toString ().replaceAll (" " , "" ).replaceAll ("\n " , "" ).length ();
565
+ int length = this .editText .getText ().toString ()
566
+ .replaceAll (" " , "" ).replaceAll ("\n " , "" ).length ();
559
567
String lengthStr = Integer .toString (length ) + " / " ;
560
568
561
569
if (this .maxCharacters > 0 ) {
@@ -694,8 +702,8 @@ public void setPrimaryColor(int colorRes) {
694
702
public void setPanelBackgroundColor (int colorRes ) {
695
703
696
704
this .panelBackgroundColor = colorRes ;
697
- (( GradientDrawable ) (( LayerDrawable ) this .panel .getBackground () )
698
- .findDrawableByLayerId ( R . id . bg_cover )). setColor ( panelBackgroundColor );
705
+ this .panel .getBackground ()
706
+ .setColorFilter ( new PorterDuffColorFilter ( colorRes , PorterDuff . Mode . SRC_IN ) );
699
707
}
700
708
701
709
/* Characters Counter Setters */
@@ -939,4 +947,63 @@ public boolean getHasFocus() {
939
947
public boolean getIsResponsiveIconColor () {
940
948
return this .isResponsiveIconColor ;
941
949
}
950
+
951
+ /**
952
+ * set EditText cursor color
953
+ */
954
+ protected static void setCursorDrawableColor (EditText _editText , int _colorRes ) {
955
+
956
+ try {
957
+ Field fCursorDrawableRes = TextView .class .getDeclaredField ("mCursorDrawableRes" );
958
+ fCursorDrawableRes .setAccessible (true );
959
+ int mCursorDrawableRes = fCursorDrawableRes .getInt (_editText );
960
+ Field fEditor = TextView .class .getDeclaredField ("mEditor" );
961
+ fEditor .setAccessible (true );
962
+ Object editor = fEditor .get (_editText );
963
+ Class <?> clazz = editor .getClass ();
964
+ Field fCursorDrawable = clazz .getDeclaredField ("mCursorDrawable" );
965
+ fCursorDrawable .setAccessible (true );
966
+ Drawable [] drawables = new Drawable [2 ];
967
+ drawables [0 ] = ContextCompat .getDrawable (_editText .getContext (), mCursorDrawableRes );
968
+ drawables [1 ] = ContextCompat .getDrawable (_editText .getContext (), mCursorDrawableRes );
969
+ drawables [0 ].setColorFilter (_colorRes , PorterDuff .Mode .SRC_IN );
970
+ drawables [1 ].setColorFilter (_colorRes , PorterDuff .Mode .SRC_IN );
971
+ fCursorDrawable .set (editor , drawables );
972
+ } catch (Throwable ignored ) {
973
+ }
974
+ }
975
+
976
+ /**
977
+ * return a lighter color
978
+ *
979
+ * @param _factor percentage of light applied
980
+ */
981
+ protected static int lighter (int color , float _factor ) {
982
+
983
+ int red = (int ) ((Color .red (color ) * (1 - _factor ) / 255 + _factor ) * 255 );
984
+ int green = (int ) ((Color .green (color ) * (1 - _factor ) / 255 + _factor ) * 255 );
985
+ int blue = (int ) ((Color .blue (color ) * (1 - _factor ) / 255 + _factor ) * 255 );
986
+ return Color .argb (Color .alpha (color ), red , green , blue );
987
+ }
988
+
989
+ protected static boolean isLight (int color ) {
990
+ return Math .sqrt (
991
+ Color .red (color ) * Color .red (color ) * .241 +
992
+ Color .green (color ) * Color .green (color ) * .691 +
993
+ Color .blue (color ) * Color .blue (color ) * .068 ) > 130 ;
994
+ }
995
+
996
+ /**
997
+ * adjust the alpha value of the color
998
+ *
999
+ * @return the color after adjustment
1000
+ */
1001
+ protected static int adjustAlpha (int color , float _toAlpha ) {
1002
+
1003
+ int alpha = Math .round (255 * _toAlpha );
1004
+ int red = Color .red (color );
1005
+ int green = Color .green (color );
1006
+ int blue = Color .blue (color );
1007
+ return Color .argb (alpha , red , green , blue );
1008
+ }
942
1009
}
0 commit comments