4
4
* Created by Swapnil Tiwari on 2019-05-07.
5
5
* swapniltiwari775@gmail.com
6
6
*/
7
+
7
8
import android .content .Context ;
8
9
import android .content .res .TypedArray ;
9
10
import android .graphics .Canvas ;
10
11
import android .graphics .Paint ;
11
12
13
+ import androidx .annotation .ColorRes ;
12
14
import androidx .annotation .Nullable ;
13
15
import androidx .core .content .ContextCompat ;
14
16
import androidx .appcompat .widget .AppCompatEditText ;
17
+
15
18
import android .text .Editable ;
16
19
import android .text .TextWatcher ;
17
20
import android .util .AttributeSet ;
@@ -29,6 +32,7 @@ public class OtpEditText extends AppCompatEditText implements TextWatcher {
29
32
private Paint mLinesPaint ;
30
33
private Paint mStrokePaint ;
31
34
private Paint mTextPaint ;
35
+ private Paint mHintPaint ;
32
36
33
37
private boolean mMaskInput ;
34
38
@@ -37,6 +41,7 @@ public class OtpEditText extends AppCompatEditText implements TextWatcher {
37
41
private int mPrimaryColor ;
38
42
private int mSecondaryColor ;
39
43
private int mTextColor ;
44
+ private int mHintTextColor ;
40
45
41
46
private float mLineStrokeSelected = 2 ; //2dp by default
42
47
private float mLineStroke = 1 ; //1dp by default
@@ -47,12 +52,16 @@ public class OtpEditText extends AppCompatEditText implements TextWatcher {
47
52
48
53
private String mBoxStyle ;
49
54
private String mMaskCharacter = "*" ;
55
+ private String mHintText = "" ;
50
56
51
57
private final String ROUNDED_BOX = "rounded_box" ;
52
58
private final String UNDERLINE = "underline" ;
53
59
private final String SQUARE_BOX = "square_box" ;
54
60
private final String ROUNDED_UNDERLINE = "rounded_underline" ;
55
61
62
+ float [] textWidths ;
63
+ float [] hintWidth = new float [1 ];
64
+
56
65
private OnCompleteListener completeListener ;
57
66
58
67
public OtpEditText (Context context ) {
@@ -79,6 +88,9 @@ private void init(Context context, @Nullable AttributeSet attrs) {
79
88
mTextPaint = getPaint ();
80
89
mTextPaint .setColor (mTextColor );
81
90
91
+ mHintPaint = new Paint (getPaint ());
92
+ mHintPaint .setColor (mHintTextColor );
93
+
82
94
// Set the TextWatcher
83
95
this .addTextChangedListener (this );
84
96
float multi = context .getResources ().getDisplayMetrics ().density ;
@@ -126,6 +138,8 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
126
138
final TypedArray a = getContext ().obtainStyledAttributes (attributeSet , R .styleable .OtpEditText , defStyleAttr , 0 );
127
139
128
140
mMaxLength = attributeSet .getAttributeIntValue (XML_NAMESPACE_ANDROID , "maxLength" , 6 );
141
+ mHintText = attributeSet .getAttributeValue (XML_NAMESPACE_ANDROID , "hint" );
142
+ mHintTextColor = attributeSet .getAttributeIntValue (XML_NAMESPACE_ANDROID , "textColorHint" , getResources ().getColor (R .color .hint_color ));
129
143
mPrimaryColor = a .getColor (R .styleable .OtpEditText_oev_primary_color , getResources ().getColor (android .R .color .holo_red_dark ));
130
144
mSecondaryColor = a .getColor (R .styleable .OtpEditText_oev_secondary_color , getResources ().getColor (R .color .light_gray ));
131
145
mTextColor = a .getColor (R .styleable .OtpEditText_oev_text_color , getResources ().getColor (android .R .color .black ));
@@ -174,13 +188,17 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
174
188
@ Nullable
175
189
public String getOtpValue () {
176
190
if (String .valueOf (getText ()).length () != mMaxLength ) {
177
- this . startAnimation ( AnimationUtils . loadAnimation ( getContext (), R . anim . shake ) );
191
+ triggerErrorAnimation ( );
178
192
return null ;
179
193
} else {
180
194
return String .valueOf (getText ());
181
195
}
182
196
}
183
197
198
+ public void triggerErrorAnimation () {
199
+ this .startAnimation (AnimationUtils .loadAnimation (getContext (), R .anim .shake ));
200
+ }
201
+
184
202
@ Override
185
203
public void setCustomSelectionActionModeCallback (ActionMode .Callback actionModeCallback ) {
186
204
throw new RuntimeException ("setCustomSelectionActionModeCallback() not supported." );
@@ -198,13 +216,32 @@ protected void onDraw(Canvas canvas) {
198
216
mLineSpacing = (float ) (getHeight () * .6 );
199
217
200
218
int startX = getPaddingLeft ();
219
+ int hintStartX = getPaddingLeft ();
201
220
int bottom = getHeight () - getPaddingBottom ();
202
221
int top = getPaddingTop ();
203
222
204
223
//Text Width
205
224
Editable text = getText ();
225
+
226
+
206
227
int textLength = text .length ();
207
- float [] textWidths = new float [textLength ];
228
+ textWidths = new float [textLength ];
229
+
230
+
231
+ if (text .length () == 0 && !mHintText .isEmpty ()) {
232
+ getPaint ().getTextWidths ("1" , 0 , 1 , hintWidth );
233
+ for (int i = 0 ; i < mNumChars && i < mHintText .length (); i ++) {
234
+ float middle = hintStartX + mCharSize / 2 ;
235
+ canvas .drawText (mHintText , i , i + 1 , middle - hintWidth [0 ] / 2 , mLineSpacing , mHintPaint );
236
+
237
+ if (mSpace < 0 ) {
238
+ hintStartX += mCharSize * 2 ;
239
+ } else {
240
+ hintStartX += mCharSize + mSpace ;
241
+ }
242
+ }
243
+ }
244
+
208
245
getPaint ().getTextWidths (getText (), 0 , textLength , textWidths );
209
246
210
247
for (int i = 0 ; i < mNumChars ; i ++) {
@@ -240,9 +277,9 @@ protected void onDraw(Canvas canvas) {
240
277
if (getText ().length () > i ) {
241
278
float middle = startX + mCharSize / 2 ;
242
279
if (mMaskInput ) {
243
- canvas .drawText (getMaskText (), i , i + 1 , middle - textWidths [0 ] / 2 , mLineSpacing , getPaint () );
280
+ canvas .drawText (getMaskText (), i , i + 1 , middle - textWidths [0 ] / 2 , mLineSpacing , mTextPaint );
244
281
} else {
245
- canvas .drawText (text , i , i + 1 , middle - textWidths [0 ] / 2 , mLineSpacing , getPaint () );
282
+ canvas .drawText (text , i , i + 1 , middle - textWidths [0 ] / 2 , mLineSpacing , mTextPaint );
246
283
}
247
284
}
248
285
@@ -251,6 +288,7 @@ protected void onDraw(Canvas canvas) {
251
288
} else {
252
289
startX += mCharSize + mSpace ;
253
290
}
291
+
254
292
}
255
293
}
256
294
0 commit comments