3
3
import android .content .ClipboardManager ;
4
4
import android .content .Context ;
5
5
import android .content .res .TypedArray ;
6
- import androidx .databinding .InverseBindingMethod ;
7
- import androidx .databinding .InverseBindingMethods ;
6
+
7
+ import androidx .annotation .ColorInt ;
8
+ import androidx .core .content .ContextCompat ;
9
+
8
10
import android .graphics .PorterDuff ;
9
11
import android .graphics .drawable .Drawable ;
10
12
import androidx .appcompat .widget .LinearLayoutCompat ;
16
18
import android .view .MenuItem ;
17
19
import android .view .View ;
18
20
import android .widget .EditText ;
21
+ import android .widget .TextView ;
19
22
20
23
24
+ import java .lang .reflect .Field ;
21
25
import java .util .ArrayList ;
22
26
23
27
@@ -30,6 +34,8 @@ public class PinView extends LinearLayoutCompat {
30
34
private int pinSize =getResources ().getDimensionPixelSize (R .dimen .pin_size ), passwordToggleSize =getResources ().getDimensionPixelSize (R .dimen .password_toggle_size );
31
35
private int passwordToggleColor =getResources ().getColor (android .R .color .black );
32
36
private float pinTextSize =DEFAULT_PIN_TEXT_SIZE ;
37
+ private int pinTextColor =getResources ().getColor (android .R .color .black );
38
+ // private int pinCursorColor =getResources().getColor(android.R.color.holo_orange_dark);
33
39
private short pinCount =DEFAULT_PIN_COUNT , inputType = com .gne .www .lib .InputType .TYPE_NUMBER ;
34
40
private boolean isPassword =false , showPasswordToggle =false , isToggleAdded =false ;
35
41
private Drawable background ;
@@ -78,6 +84,8 @@ private void setStyleAndPins(AttributeSet attrs){
78
84
pinTextSize =a .getDimension (R .styleable .PinView_pinTextSize ,DEFAULT_PIN_TEXT_SIZE );
79
85
passwordToggleSize =a .getDimensionPixelSize (R .styleable .PinView_passwordToggleSize ,passwordToggleSize );
80
86
passwordToggleColor =a .getColor (R .styleable .PinView_passwordToggleColor ,passwordToggleColor );
87
+ pinTextColor =a .getColor (R .styleable .PinView_textColor ,pinTextColor );
88
+ // pinCursorColor =a.getColor(R.styleable.PinView_cursorColor,pinCursorColor);
81
89
82
90
if (a .hasValue (R .styleable .PinView_pinBackground )){
83
91
background =a .getDrawable (R .styleable .PinView_pinBackground );
@@ -114,6 +122,8 @@ private void addPins(){
114
122
getResources ().getDimensionPixelSize (R .dimen .margin_pin_edit_text ),getResources ().getDimensionPixelSize (R .dimen .margin_pin_edit_text ));
115
123
editText .setLayoutParams (layoutParams );
116
124
editText .setTextSize (pinTextSize );
125
+ editText .setTextColor (pinTextColor );
126
+ // setCursorColor(editText,pinCursorColor);
117
127
editText .setMaxLines (1 );
118
128
editText .setLines (1 );
119
129
editText .setPadding (0 ,0 ,0 ,0 );
@@ -391,6 +401,28 @@ public void setPinTextSize(float textSize){
391
401
}
392
402
}
393
403
404
+ /**
405
+ * Text color of the pins
406
+ * @param textColor
407
+ */
408
+ public void setTextColor (int textColor ){
409
+ pinTextColor =textColor ;
410
+ for (int i =0 ; i <getPinCount (); i ++){
411
+ editTextsArrayList .get (i ).setTextColor (pinTextColor );
412
+ }
413
+ }
414
+
415
+ /**
416
+ * Cursor color of the pins
417
+ * @param cursorColor
418
+ */
419
+ // public void setCursorColor(int cursorColor){
420
+ // pinCursorColor=cursorColor;
421
+ // for (int i=0; i<getPinCount(); i++){
422
+ // setCursorColor(editTextsArrayList.get(i),pinCursorColor);
423
+ // }
424
+ // }
425
+
394
426
/**
395
427
* Size of the toggle view
396
428
* @param toggleSize size in pixels
@@ -419,5 +451,28 @@ public void setPasswordToggleColor(int color){
419
451
}
420
452
}
421
453
422
-
454
+ private void setCursorColor (EditText view , @ ColorInt int color ) {
455
+ try {
456
+ // Get the cursor resource id
457
+ Field field = TextView .class .getDeclaredField ("mCursorDrawableRes" );
458
+ field .setAccessible (true );
459
+ int drawableResId = field .getInt (view );
460
+
461
+ // Get the editor
462
+ field = TextView .class .getDeclaredField ("mEditor" );
463
+ field .setAccessible (true );
464
+ Object editor = field .get (view );
465
+
466
+ // Get the drawable and set a color filter
467
+ Drawable drawable = ContextCompat .getDrawable (view .getContext (), drawableResId );
468
+ drawable .setColorFilter (color , PorterDuff .Mode .SRC_IN );
469
+ Drawable [] drawables = {drawable , drawable };
470
+
471
+ // Set the drawables
472
+ field = editor .getClass ().getDeclaredField ("mCursorDrawable" );
473
+ field .setAccessible (true );
474
+ field .set (editor , drawables );
475
+ } catch (Exception ignored ) {
476
+ }
477
+ }
423
478
}
0 commit comments